1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  |     // 设计思想是, 用一个lock代表打印资格, 谁获取到谁可以打印; 
    // 并且用一个state来控制打印顺序, 一个线程打印完后, 将state变为下一个状态,
    // 即,state在 a->b->c->a->b->...控制当前应打印字符的流转;
public class Test {
    private static volatile Integer currentState = 1;
    private static ReentrantLock lock = new ReentrantLock();
    // 一个condition对应一个等待队列, 一个lock可以绑定多个condition, 即支持多个等待队列.
    // 相比synchronized,有多个等待队列, 通知下一个队列时也有针对性, 即 竞争效率比较高
    // 效率最高的应该还是semaphore
    private static Condition conditionA = lock.newCondition();
    private static Condition conditionB = lock.newCondition();
    private static Condition conditionC = lock.newCondition();
    public static void main(String[] args) throws InterruptedException {
        ExecutorService poolSerivce = Executors.newFixedThreadPool(3);
        Integer count = 10;
        poolSerivce.execute(new Worker("A", 1, 2, count, lock, conditionA, conditionB));
        poolSerivce.execute(new Worker("B", 2, 3, count, lock, conditionB, conditionC));
        poolSerivce.execute(new Worker("C", 3, 1, count, lock, conditionC, conditionA));
        Thread.sleep(1000);
        poolSerivce.shutdown();
    }
    public static class Worker implements Runnable {
        private String key;
        //本线程期待的state
        private int targetState;
        private int nextState;
        private Integer count;
        private Lock lock;
        private Condition current;
        private Condition next;
        public Worker(String key, int targetState, int nextState, Integer count, Lock lock, Condition current, Condition next) {
            this.key = key;
            this.targetState = targetState;
            this.nextState = nextState;
            this.count = count;
            this.lock = lock;
            this.current = current;
            this.next = next;
        }
        @Override
        public void run() {
            this.lock.lock();
            try {
                for (int i = 0; i < count; i++) {
                    // 等待流转到目标状态
                    while (currentState != targetState) {
                        // 放进当前的等待队列中
                        current.await();
                    }
                    System.out.print(key);
                    currentState = nextState;
                    // 通知下一个等待队列
                    next.signal();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                this.lock.unlock();
            }
        }
    }
}
  |