多线程-按顺序输出ABCABC

巧妙方法1 利用数组的余数的特性

public class Myprint3 { private int i = 0;

public static void main(String[] args) {
    Myprint3 myprint3 = new Myprint3();
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
               synchronized (myprint3){
                   if(myprint3.i%3==0){
                       System.out.println(Thread.currentThread().getName()+"A");
                       myprint3.myPirnt();
                   }
               }
            }
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                synchronized (myprint3){
                    if(myprint3.i%3==1){
                        System.out.println(Thread.currentThread().getName()+"B");
                        myprint3.myPirnt();
                    }
                }
            }
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                synchronized (myprint3){
                    if(myprint3.i%3==2){
                        System.out.println(Thread.currentThread().getName()+"C");
                        myprint3.myPirnt();
                    }
                }
            }
        }
    }).start();
}

public void myPirnt() {
    i++;

}
复制代码

}

常规方法 使用三个信号量,开控制

public int i; public boolean a = true; public boolean b = false; public boolean c = false;

public static void main(String[] args) {
    Myprint myprint = new Myprint();

    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                synchronized (myprint) {
                    while (myprint.a) {
                        myprint.getAdd();
                        myprint.a=false;
                        myprint.b=true;
                        myprint.c=false;
                    }
                }
            }
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                synchronized (myprint) {
                    while (myprint.b) {
                        myprint.getAdd();
                        myprint.a=false;
                        myprint.b=false;
                        myprint.c=true;
                    }
                }
            }
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                synchronized (myprint) {
                    while (myprint.c) {
                        myprint.getAdd();
                        myprint.a=true;
                        myprint.b=false;
                        myprint.c=false;
                    }
                }
            }
        }
    }).start();
}

public void getAdd() {
    for (int j = 0; j < 5; j++) {
        i++;
        System.out.println(Thread.currentThread().getName()+i);
    }
}复制代码

转载于:https://juejin.im/post/5cdd246ee51d456e79545d70

你可能感兴趣的:(多线程-按顺序输出ABCABC)