java多线程,三个线程轮流执行顺序打印ABC,循环5次。使用三个Condition实现。

package com.zhong.thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 三个线程依次输出A,B,C,循环5次。 使用Condition实现
 * 输出结果:
 * ABC
 * ABC
 * ABC
 * ABC
 * ABC
 */
public class SequencePrintDemo {

    private static volatile int newPrintWho = 1; //下一个输出

    private static ReentrantLock lock = new ReentrantLock();

    private static final Condition conditionA = lock.newCondition();
    private static final Condition conditionB = lock.newCondition();
    private static final Condition conditionC = lock.newCondition();

    private static final int TIMES = 5; //循环次数

    public static void main(String[] args) {
        Thread threadA = new Thread() {
            @Override
            public void run() {

                for (int i = 0; i < TIMES; i++) {
                    try{
                        lock.lock();
                        while (newPrintWho !=1){
                            conditionA.await(); //阻塞
                        }
                        System.out.print("A");
                        newPrintWho = 2;

                        conditionB.signalAll(); //唤醒ThreadB
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }finally {
                        lock.unlock();
                    }
                }

            }
        };

        Thread threadB = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < TIMES; i++) {
                    try{
                        lock.lock();
                        while (newPrintWho !=2){
                            conditionB.await(); //阻塞
                        }
                        System.out.print("B");
                        newPrintWho = 3;
                        conditionC.signalAll(); //唤醒ThreadC
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }finally {
                        lock.unlock();
                    }
                }
            }
        };
        Thread threadC = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < TIMES; i++) {
                    try{
                        lock.lock();
                        while (newPrintWho !=3){
                            conditionC.await(); //阻塞
                        }

                        System.out.println("C");
                        newPrintWho = 1;
                        conditionA.signalAll(); //唤醒ThreadA
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }finally {
                        lock.unlock();
                    }
                }
            }
        };


        threadA.start();
        threadB.start();
        threadC.start();

    }

}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页