java 并发编程(2)

  • 时间:2019-06-11 03:49 作者:zidea 来源:zidea 阅读:56
  • 扫一扫,手机访问
摘要:java_thread原本想都以做菜开饭来讲解这个套 java 并发编程,不过对于那些不做菜的朋友有点太乏味了,也缺乏亲自体验,所以还是多元化,关键是以做菜为例进行讲解在配上美食图,会不会听到不是知识饥渴,而真的变得饥渴了。starcraft barracks这个游戏是我的时代游戏,一听就能猜出我大
java_thread

原本想都以做菜开饭来讲解这个套 java 并发编程,不过对于那些不做菜的朋友有点太乏味了,也缺乏亲自体验,所以还是多元化,关键是以做菜为例进行讲解在配上美食图,会不会听到不是知识饥渴,而真的变得饥渴了。

starcraft barracks

这个游戏是我的时代游戏,一听就能猜出我大概哪个时代的人了? 也不肯定。游戏过程中我们获取资源,用资源建造兵营,兵营负责出兵进行战斗。

兵营的任务就是建造士兵,所以可以将线程比喻成兵营,代码如下。

  • 玩过游戏的人都知道兵数量是有上限的,这里给 50.
  • 而且兵是一个一个出。
public class Barracks extends Thread{    private final String name;    private final int MAX = 50;    private int index = 1;    public Barracks(String name) {        this.name = name;    }    @Override    public void run() {        while(index <= MAX){            System.out.println("barrack  " + name + " create warrior: " + (index++));        }    }}
public class ClientB {    public static void main(String[] args) {        Barracks barracks = new Barracks("one");        barracks.start();    }}

为了快速出兵,我们可以建造多个兵营同时出兵。

    public static void main(String[] args) {        Barracks barrackOne = new Barracks("one");        barrackOne.start();        Barracks barrackTwo = new Barracks("two");        barrackTwo.start();        Barracks barrackThree = new Barracks("three");        barrackThree.start();    }

不过从输出来看,由于出兵数量控制业务逻辑放置兵营(线程),这样就带来问题。对兵数量控制应该交个种族而不是兵营

barrack  two create warrior: 2barrack  three create warrior: 3barrack  one create warrior: 1barrack  three create warrior: 5barrack  two create warrior: 4barrack  three create warrior: 7barrack  one create warrior: 6barrack  three create warrior: 9barrack  two create warrior: 8barrack  three create warrior: 11barrack  one create warrior: 10barrack  three create warrior: 13barrack  two create warrior: 12barrack  three create warrior: 15barrack  one create warrior: 14barrack  three create warrior: 17barrack  two create warrior: 16barrack  two create warrior: 20barrack  two create warrior: 21barrack  three create warrior: 19barrack  one create warrior: 18barrack  three create warrior: 23barrack  three create warrior: 25barrack  two create warrior: 22barrack  three create warrior: 26barrack  one create warrior: 24barrack  three create warrior: 28barrack  two create warrior: 27barrack  three create warrior: 30barrack  one create warrior: 29barrack  three create warrior: 32barrack  two create warrior: 31barrack  three create warrior: 34barrack  three create warrior: 36barrack  one create warrior: 33barrack  one create warrior: 38barrack  three create warrior: 37barrack  three create warrior: 40barrack  three create warrior: 41barrack  two create warrior: 35barrack  three create warrior: 42barrack  one create warrior: 39barrack  three create warrior: 44barrack  two create warrior: 43barrack  three create warrior: 46barrack  one create warrior: 45barrack  one create warrior: 49barrack  one create warrior: 50barrack  three create warrior: 48barrack  two create warrior: 47

我们可以把这些业务逻辑冲一个个线程从提取到 Runnable 接口以达到多线程共享。

public class BarrackRunnable implements Runnable{    private int index = 0;    private final static int MAX = 50;    @Override    public void run() {        while(index <= MAX){            System.out.println("barrack  " + Thread.currentThread().getName() + " create warrior: " + (index++));        }    }}
public class ClientC {    public static void main(String[] args) {        final BarrackRunnable barrackRunnable = new BarrackRunnable();        Thread barrackThreadOne = new Thread(barrackRunnable,"one");        Thread barrackThreadTwo = new Thread(barrackRunnable,"two");        Thread barrackThreadThree = new Thread(barrackRunnable,"three");        barrackThreadOne.start();        barrackThreadTwo.start();        barrackThreadThree.start();    }}
barrack  one create warrior: 0barrack  one create warrior: 3barrack  one create warrior: 4barrack  one create warrior: 5barrack  one create warrior: 6barrack  one create warrior: 7barrack  one create warrior: 8barrack  one create warrior: 9barrack  one create warrior: 10barrack  one create warrior: 11barrack  one create warrior: 12barrack  one create warrior: 13barrack  one create warrior: 14barrack  one create warrior: 15barrack  one create warrior: 16barrack  one create warrior: 17barrack  one create warrior: 18barrack  two create warrior: 1barrack  two create warrior: 20barrack  two create warrior: 21barrack  two create warrior: 22barrack  one create warrior: 19barrack  one create warrior: 24barrack  one create warrior: 25barrack  one create warrior: 26barrack  one create warrior: 27barrack  one create warrior: 28barrack  one create warrior: 29barrack  one create warrior: 30barrack  one create warrior: 31barrack  two create warrior: 23barrack  two create warrior: 33barrack  two create warrior: 34barrack  one create warrior: 32barrack  one create warrior: 36barrack  two create warrior: 35barrack  two create warrior: 38barrack  two create warrior: 39barrack  one create warrior: 37barrack  one create warrior: 41barrack  two create warrior: 40barrack  two create warrior: 43barrack  one create warrior: 42barrack  one create warrior: 45barrack  one create warrior: 46barrack  one create warrior: 47barrack  one create warrior: 48barrack  one create warrior: 49barrack  one create warrior: 50barrack  two create warrior: 44barrack  three create warrior: 2
thread
  • 全部评论(0)
最新发布的资讯信息
【系统环境|windows】字节跳动前台面试题解析:盛最多水的容器(2021-03-20 21:27)
【系统环境|windows】DevOps敏捷60问,肯定有你想理解的问题(2021-03-20 21:27)
【系统环境|windows】字节跳动最爱考的前台面试题:JavaScript 基础(2021-03-20 21:27)
【系统环境|windows】JavaScript 的 switch 条件语句(2021-03-20 21:27)
【系统环境|windows】解决 XML 数据应用实践(2021-03-20 21:26)
【系统环境|windows】20个编写现代CSS代码的建议(2021-03-20 21:26)
【系统环境|windows】《vue 3.0探险记》- 运行报错:Error:To install them, you can run: npm install --save core-js/modules/es.arra...(2021-03-20 21:24)
【系统环境|windows】浅谈前台可视化编辑器的实现(2021-03-20 21:24)
【系统环境|windows】产品经理入门迁移学习指南(2021-03-20 21:23)
【系统环境|windows】初识webRTC(2021-03-20 21:23)
血鸟云
手机二维码手机访问领取大礼包
返回顶部