美文网首页
JUC(八) - 线程按序交替

JUC(八) - 线程按序交替

作者: 21号新秀_邓肯 | 来源:发表于2020-05-18 15:47 被阅读0次

    8. 线程按序交替

    编写一个程序,开启 3 个线程,这三个线程的 ID 分别为A、B、C,每个线程将自己的 ID 在屏幕上打印 10 遍,要求输出的结果必须按顺序显示。

    class AlternateDemo {
        /**
         * 标记启动顺序
         */
        private int number = 1;
    
        private Lock lock = new ReentrantLock();
    
        private Condition condition1 = lock.newCondition();
        private Condition condition2 = lock.newCondition();
        private Condition condition3 = lock.newCondition();
    
        /**
         * A线程
         * @param totalLoop
         */
        public void loopA(int totalLoop) {
            lock.lock();
    
            try {
                //1.判断
                if (number != 1) {
                    condition1.await();
                }
                //2.打印
                for (int i = 1; i <= 1; i++) {
                    System.out.println(Thread.currentThread().getName() + "\t" + i + "\t" + totalLoop);
                }
    
                //3.唤醒
                number = 2;
                condition2.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    
        /**
         * B线程
         * @param totalLoop
         */
        public void loopB(int totalLoop) {
            lock.lock();
    
            try {
                //1.判断
                if (number != 2) {
                    condition2.await();
                }
                //2.打印
                for (int i = 1; i <= 1; i++) {
                    System.out.println(Thread.currentThread().getName() + "\t" + i + "\t" + totalLoop);
                }
    
                //3.唤醒
                number = 3;
                condition3.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    
        /**
         * C线程
         * @param totalLoop
         */
        public void loopC(int totalLoop) {
            lock.lock();
    
            try {
                //1.判断
                if (number != 3) {
                    condition3.await();
                }
                //2.打印
                for (int i = 1; i <= 1; i++) {
                    System.out.println(Thread.currentThread().getName() + "\t" + i + "\t" + totalLoop);
                }
    
                //3.唤醒
                number = 1;
                condition1.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
    

    测试

        public static void main(String[] args) {
            final AlternateDemo ad  = new AlternateDemo();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 1; i <= 5; i++) {
                        ad.loopA(i);
                    }
                }
            }, "A").start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 1; i <= 5; i++) {
                        ad.loopB(i);
                    }
                }
            }, "B").start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 1; i <= 5; i++) {
                        ad.loopC(i);
                    }
                }
            }, "C").start();
        }
    

    相关文章

      网友评论

          本文标题:JUC(八) - 线程按序交替

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