分享

Java中Condition的使用

 liang1234_ 2020-10-23

Java架构师交流群:793825326

java版本:jdk1.8

IDE:idea 18

之前的一篇博客(https://blog.csdn.net/dap769815768/article/details/96712257)讲了wait、notify\notifyAll的使用,实际上目前我们比较推荐的方式是用Condition的await、signal\signalAll来实现类似的功能。

添加一个测试类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. public void start1() {
  6. try {
  7. lock.lock();
  8. System.out.println(Thread.currentThread().getId() + "执行");
  9. condition1.await();
  10. System.out.println(Thread.currentThread().getId() + "start");
  11. Thread.sleep(5000);
  12. System.out.println(Thread.currentThread().getId() + "end");
  13. lock.unlock();
  14. } catch (Exception ex) {
  15. System.out.println(ex);
  16. }
  17. }
  18. public void start2() {
  19. try
  20. {
  21. lock.lock();
  22. System.out.println(Thread.currentThread().getId() + "执行");
  23. condition2.await();
  24. System.out.println(Thread.currentThread().getId() + "start");
  25. Thread.sleep(5000);
  26. System.out.println(Thread.currentThread().getId() + "end");
  27. lock.unlock();
  28. } catch (Exception ex) {
  29. System.out.println(ex);
  30. }
  31. }
  32. public void releaseAll()
  33. {
  34. lock.lock();
  35. condition1.signalAll();
  36. condition2.signalAll();
  37. lock.unlock();
  38. }
  39. public synchronized void release1()
  40. {
  41. lock.lock();
  42. condition1.signal();
  43. lock.unlock();
  44. }
  45. public synchronized void release2()
  46. {
  47. lock.lock();
  48. condition2.signal();
  49. lock.unlock();
  50. }
  51. }

测试代码如下:

  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的方式,显然比之前控制地更精确了。

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多