Java中Condition的使用
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Java中Condition的使⽤Java架构师交流群:793825326
java版本:jdk1.8
IDE:idea 18
添加⼀个测试类Test:
1. public class Test {
2. private Lock lock=new ReentrantLock();
3. private Condition condition=lock.newCondition();
4. public void start() {
5. try {
6. lock.lock();
7. System.out.println(Thread.currentThread().getId() + "执⾏");
8. condition.await();
9. System.out.println(Thread.currentThread().getId() + "start");
10. Thread.sleep(5000);
11. System.out.println(Thread.currentThread().getId() + "end");
12. lock.unlock();
13. } catch (Exception ex) {
14. System.out.println(ex);
15. }
16. }
17. public void releaseAll()
18. {
19. lock.lock();
20. condition.signalAll();
21. lock.unlock();
22. }
23. public synchronized void releaseOne()
24. {
25. lock.lock();
26. condition.signal();
27. lock.unlock();
28. }
29. }
写⼀段测试代码:
1. Test t=new Test();
2. try {
3. Thread thread1= new Thread(() -> t.start());
4. thread1.start();
5. Thread.sleep(100);
6. Thread thread2= new Thread(() -> t.start());
7. thread2.start();
8. Thread.sleep(1000);
9. t.releaseAll();
10. }
11. catch (Exception ex)
12. {
13. System.out.println(ex);
14. }
这和之前使⽤wait、notify\notifyAll这套⽅案实现的效果⼀样。
如果单纯从这个例⼦来看,它们之间并没有什么区别,Condition的优势也没有体现出来。
实际上Condition可以实现更细粒度的控制。
看下⾯的代码:
1. public class Test {
2. private Lock lock=new ReentrantLock();
3. private Condition condition1=lock.newCondition();
4. private Condition condition2=lock.newCondition();
5.
6. public void start1() {
7. try {
8. lock.lock();
9. System.out.println(Thread.currentThread().getId() + "执⾏");
10. condition1.await();
11. System.out.println(Thread.currentThread().getId() + "start");
12. Thread.sleep(5000);
13. System.out.println(Thread.currentThread().getId() + "end");
14. lock.unlock();
15. } catch (Exception ex) {
16. System.out.println(ex);
17. }
18. }
19.
20. public void start2() {
21. try
22. {
23. lock.lock();
24. System.out.println(Thread.currentThread().getId() + "执⾏");
25. condition2.await();
26. System.out.println(Thread.currentThread().getId() + "start");
27. Thread.sleep(5000);
28. System.out.println(Thread.currentThread().getId() + "end");
29. lock.unlock();
30. } catch (Exception ex) {
31. System.out.println(ex);
32. }
33. }
34.
35. public void releaseAll()
36. {
37. lock.lock();
38. condition1.signalAll();
39. condition2.signalAll();
40. lock.unlock();
41. }
42. public synchronized void release1()
43. {
44. lock.lock();
45. condition1.signal();
46. lock.unlock();
47. }
48. public synchronized void release2()
49. {
50. lock.lock();
51. condition2.signal();
52. lock.unlock();
53. }
54. }
测试代码如下:
1. Test t=new Test();
2. try {
3. Thread thread1= new Thread(() -> t.start1());
4. thread1.start();
5. Thread.sleep(100);
6. Thread thread2= new Thread(() -> t.start2());
7. thread2.start();
8. Thread.sleep(1000);
9. t.release2();
10. t.release1();
11. }
12. catch (Exception ex)
13. {
14. System.out.println(ex);
15. }
它的执⾏结果:
14执⾏
15执⾏
15start
15end
14start
14end
在这个例⼦中,采⽤Condition的⽅式,显然⽐之前控制地更精确了。