java 并发编程(2)

作者: zidea | 来源:发表于2019-05-05 10:47 被阅读10次
    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: 2
    barrack  three create warrior: 3
    barrack  one create warrior: 1
    barrack  three create warrior: 5
    barrack  two create warrior: 4
    barrack  three create warrior: 7
    barrack  one create warrior: 6
    barrack  three create warrior: 9
    barrack  two create warrior: 8
    barrack  three create warrior: 11
    barrack  one create warrior: 10
    barrack  three create warrior: 13
    barrack  two create warrior: 12
    barrack  three create warrior: 15
    barrack  one create warrior: 14
    barrack  three create warrior: 17
    barrack  two create warrior: 16
    barrack  two create warrior: 20
    barrack  two create warrior: 21
    barrack  three create warrior: 19
    barrack  one create warrior: 18
    barrack  three create warrior: 23
    barrack  three create warrior: 25
    barrack  two create warrior: 22
    barrack  three create warrior: 26
    barrack  one create warrior: 24
    barrack  three create warrior: 28
    barrack  two create warrior: 27
    barrack  three create warrior: 30
    barrack  one create warrior: 29
    barrack  three create warrior: 32
    barrack  two create warrior: 31
    barrack  three create warrior: 34
    barrack  three create warrior: 36
    barrack  one create warrior: 33
    barrack  one create warrior: 38
    barrack  three create warrior: 37
    barrack  three create warrior: 40
    barrack  three create warrior: 41
    barrack  two create warrior: 35
    barrack  three create warrior: 42
    barrack  one create warrior: 39
    barrack  three create warrior: 44
    barrack  two create warrior: 43
    barrack  three create warrior: 46
    barrack  one create warrior: 45
    barrack  one create warrior: 49
    barrack  one create warrior: 50
    barrack  three create warrior: 48
    barrack  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: 0
    barrack  one create warrior: 3
    barrack  one create warrior: 4
    barrack  one create warrior: 5
    barrack  one create warrior: 6
    barrack  one create warrior: 7
    barrack  one create warrior: 8
    barrack  one create warrior: 9
    barrack  one create warrior: 10
    barrack  one create warrior: 11
    barrack  one create warrior: 12
    barrack  one create warrior: 13
    barrack  one create warrior: 14
    barrack  one create warrior: 15
    barrack  one create warrior: 16
    barrack  one create warrior: 17
    barrack  one create warrior: 18
    barrack  two create warrior: 1
    barrack  two create warrior: 20
    barrack  two create warrior: 21
    barrack  two create warrior: 22
    barrack  one create warrior: 19
    barrack  one create warrior: 24
    barrack  one create warrior: 25
    barrack  one create warrior: 26
    barrack  one create warrior: 27
    barrack  one create warrior: 28
    barrack  one create warrior: 29
    barrack  one create warrior: 30
    barrack  one create warrior: 31
    barrack  two create warrior: 23
    barrack  two create warrior: 33
    barrack  two create warrior: 34
    barrack  one create warrior: 32
    barrack  one create warrior: 36
    barrack  two create warrior: 35
    barrack  two create warrior: 38
    barrack  two create warrior: 39
    barrack  one create warrior: 37
    barrack  one create warrior: 41
    barrack  two create warrior: 40
    barrack  two create warrior: 43
    barrack  one create warrior: 42
    barrack  one create warrior: 45
    barrack  one create warrior: 46
    barrack  one create warrior: 47
    barrack  one create warrior: 48
    barrack  one create warrior: 49
    barrack  one create warrior: 50
    barrack  two create warrior: 44
    barrack  three create warrior: 2
    
    thread

    相关文章

      网友评论

        本文标题:java 并发编程(2)

        本文链接:https://www.haomeiwen.com/subject/pucfoqtx.html