Thread communication java.util.concurrent.locks.Condition 用法(二)

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

public class ConditionTest2 {

    public static void main(String[] args) {
        final Bussiness bussiness = new Bussiness();
        final int times = 5;
        Thread a = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < times; i++) {
                    bussiness.forThreadA();
                }
            }
        });
        a.setName("ThreadA");

        Thread b = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < times; i++) {
                    bussiness.forThreadB();
                }
            }
        });
        b.setName("ThreadB");
        
        Thread c = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < times; i++) {
                    bussiness.forThreadC();
                }
            }
        });
        c.setName("ThreadC");
        a.start();
        b.start();
        c.start();

    }

    static class Bussiness {
        private char shouldRun = 'A';
        private Lock lock = new ReentrantLock();
        Condition conditionA = lock.newCondition();
        Condition conditionB = lock.newCondition();
        Condition conditionC = lock.newCondition();

        public void forThreadA() {
            lock.lock();
            if (shouldRun != 'A') {
                try {
                    conditionA.await();
                } catch (InterruptedException e) {
                }
            }
            for (int i = 1; i <= 1; i++) {
                System.out.println(Thread.currentThread().getName() + " " + i);
            }
            shouldRun = 'B';
            conditionB.signal();
            lock.unlock();
        }

        public void forThreadB() {
            lock.lock();
            while (shouldRun != 'B') {
                try {
                    conditionB.await();
                } catch (InterruptedException e) {
                }
            }
            for (int i = 1; i <= 2; i++) {
                System.out.println(Thread.currentThread().getName() + " " + i);
            }
            shouldRun = 'C';
            conditionC.signal();
            lock.unlock();
            
        }

        public void forThreadC() {
            lock.lock();
            while (shouldRun != 'C') {
                try {
                    conditionC.await();
                } catch (InterruptedException e) {
                }
            }
            for (int i = 1; i <= 3; i++) {
                System.out.println(Thread.currentThread().getName() + " " + i);
            }
            shouldRun = 'A';
            conditionA.signal();
            lock.unlock();
        }
    }

}

output:

ThreadA 1
ThreadB 1
ThreadB 2
ThreadC 1
ThreadC 2
ThreadC 3
ThreadA 1
ThreadB 1
ThreadB 2
ThreadC 1
ThreadC 2
ThreadC 3
ThreadA 1
ThreadB 1
ThreadB 2
ThreadC 1
ThreadC 2
ThreadC 3
ThreadA 1
ThreadB 1
ThreadB 2
ThreadC 1
ThreadC 2
ThreadC 3
ThreadA 1
ThreadB 1
ThreadB 2
ThreadC 1
ThreadC 2
ThreadC 3

原文地址:https://www.cnblogs.com/zhonghan/p/3221433.html