1 import java.util.concurrent.locks.Condition;
2 import java.util.concurrent.locks.Lock;
3 import java.util.concurrent.locks.ReentrantLock;
4
5 /**
6 * Java5条件阻塞Condition的应用
7 * Condition的功能类似在传统线程技术中的Object.wait()和Object.notify()功能。
8 * 在等待Condition时,允许发生“虚假唤醒”,这通常作为基础平台语义的让步。对于大多数应用程序,
9 * 这带来的实际影响很小,因为Condition应该总是在一个循环中被等待,并测试正被等待的状态声明。
10 * 某个实现可以随意溢出可能的虚假唤醒,但建议应用程序员总是假定这些虚假唤醒可能发生,因此总是在一个循环中等待。
11 *
12 * 一个锁内部可以有多个Condition,即有多个
13 *
14 * Condition是基于Lock之上的
15 * @author LiTaiQing
16 *
17 */
18 public class ConditionCommunication {
19
20 public static void main(String[] args) {
21 final Business business = new Business();
22 new Thread(new Runnable() {
23 @Override
24 public void run() {
25 for (int i = 1; i <= 50; i++) {
26 business.sub(i);
27 }
28
29 }
30 }).start();
31
32 for (int i = 1; i <= 50; i++) {
33 business.main(i);
34 }
35
36 }
37
38 static class Business {
39 Lock lock = new ReentrantLock();
40 Condition condition = lock.newCondition();
41 private boolean bShouldSub = true;
42
43 public void sub(int i) {
44 lock.lock();
45 try {
46 while (!bShouldSub) {
47 try {
48 condition.await();
49 } catch (Exception e) {
50 e.printStackTrace();
51 }
52 }
53 for (int j = 1; j <= 10; j++) {
54 System.out.println("sub thread sequence of " + j
55 + ",loop of " + i);
56 }
57 bShouldSub = false;
58 condition.signal();
59 } finally {
60 lock.unlock();
61 }
62 }
63
64 public void main(int i) {
65 lock.lock();
66 try {
67 while (bShouldSub) {
68 try {
69 condition.await();
70 } catch (Exception e) {
71 e.printStackTrace();
72 }
73 }
74 for (int j = 1; j <= 100; j++) {
75 System.out.println("main thread sequence of " + j
76 + ",loop of " + i);
77 }
78 bShouldSub = true;
79 condition.signal();
80 } finally {
81 lock.unlock();
82 }
83 }
84 }
85 }
1 import java.util.concurrent.locks.Condition;
2 import java.util.concurrent.locks.Lock;
3 import java.util.concurrent.locks.ReentrantLock;
4
5 public class ThreeConditionCommunication {
6 public static void main(String[] args) {
7 final Business business = new Business();
8 new Thread(new Runnable() {
9 @Override
10 public void run() {
11 for (int i = 1; i <= 50; i++) {
12 business.sub2(i);
13 }
14
15 }
16 }).start();
17 new Thread(new Runnable() {
18 @Override
19 public void run() {
20 for (int i = 1; i <= 50; i++) {
21 business.sub3(i);
22 }
23
24 }
25 }).start();
26 for (int i = 1; i <= 50; i++) {
27 business.main(i);
28 }
29
30 }
31
32 static class Business {
33 Lock lock = new ReentrantLock();
34 Condition condition1 = lock.newCondition();
35 Condition condition2 = lock.newCondition();
36 Condition condition3 = lock.newCondition();
37 private int shouldSub = 1;
38 public void sub2(int i) {
39 lock.lock();
40 try {
41 while (shouldSub != 2) {
42 try {
43 condition2.await();
44 } catch (Exception e) {
45 e.printStackTrace();
46 }
47 }
48 for (int j = 1; j <= 10; j++) {
49 System.out.println("sub2 thread sequence of " + j
50 + ",loop of " + i);
51 }
52 shouldSub = 3;
53 condition3.signal();
54 } finally {
55 lock.unlock();
56 }
57 }
58
59 public void sub3(int i) {
60 lock.lock();
61 try {
62 while (shouldSub != 3) {
63 try {
64 condition3.await();
65 } catch (Exception e) {
66 e.printStackTrace();
67 }
68 }
69 for (int j = 1; j <= 20; j++) {
70 System.out.println("sub3 thread sequence of " + j
71 + ",loop of " + i);
72 }
73 shouldSub = 1;
74 condition1.signal();
75 } finally {
76 lock.unlock();
77 }
78 }
79
80 public void main(int i) {
81 lock.lock();
82 try {
83 while (shouldSub != 1) {
84 try {
85 condition1.await();
86 } catch (Exception e) {
87 e.printStackTrace();
88 }
89 }
90 for (int j = 1; j <= 100; j++) {
91 System.out.println("main thread sequence of " + j
92 + ",loop of " + i);
93 }
94 shouldSub = 2;
95 condition2.signal();
96 } finally {
97 lock.unlock();
98 }
99 }
100
101 }
102 }