关键词搜索

源码搜索 ×
×

JAVA线程间协作:Condition

发布2016-01-01浏览6100次

详情内容

欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。

欢迎跳转到本文的原文链接:https://honeypps.com/java/java-multi-thread-of-condition/

    内置条件队列存在一些缺陷。每个内置锁都只能有一个相关联的条件队列,因而在像BounderBuffer这种类中,多个线程可能在同一个条件队列上等待不同的条件谓词,并且在最常见的加锁模式下公开条件队列对象。这些因素都使得无法满足在使用notifyAll时所有等待线程为统一类型的需求。如果想编写一个带有多个条件谓词的并发对象,或者想获得除了条件队列可见性之外的更多控制权,就可以使用显示的Lock和Condition而不是内置锁和条件队列,这是一种更灵活的选择。

    一个Condition和一个Lock关联在一起,就想一个条件队列和一个内置锁相关联一样。要创建一个Condition,可以在相关联的Lock上调用Lock.newCondition方法。正如Lock比内置加锁提供了更为丰富的功能,Condition同样比内置条件队列提供了更丰富的功能:在每个锁上可存在多个等待、条件等待可以是可中断的或者不可中断的、基于时限的等待,以及公平的或非公平的队列操作。

    与内置条件队列不同的是,对于每个Lock,可以有任意数量的Condition对象。Condition对象继承了相关的Lock对象的公平性,对于公平的锁,线程会依照FIFO顺序从Condition.await中释放。

Condition接口:

 

  1. public interface Condition{
  2. void await() throws InterruptedException;
  3. boolean await(long time, TimeUnit unit) throws InterruptedException;
  4. long awaitNanos(long nanosTimeout) throws InterruptedException;
  5. void awaitUniterruptibly();
  6. boolean awaitUntil(Date deadline) throws InterruptedException;
  7. void signal();
  8. void signalAll();
  9. }

    注意:在Condition对象中,与wait,notify和notifyAll方法对于的分别是await,signal,signalAll。但是,Condition对Object进行了扩展,因而它也包含wait和notify方法。一定要确保使用的版本——await和signal.
官方使用方法如下:

  1. class BoundedBuffer
  2. {
  3. final Lock lock = new ReentrantLock();
  4. final Condition notFull = lock.newCondition();
  5. final Condition notEmpty = lock.newCondition();
  6. final Object[] items = new Object[100];
  7. int putptr, takeptr, count;
  8. public void put(Object x) throws InterruptedException
  9. {
  10. lock.lock();
  11. try
  12. {
  13. while (count == items.length)
  14. notFull.await();
  15. items[putptr] = x;
  16. if (++putptr == items.length)
  17. putptr = 0;
  18. ++count;
  19. notEmpty.signal();
  20. }
  21. finally
  22. {
  23. lock.unlock();
  24. }
  25. }
  26. public Object take() throws InterruptedException
  27. {
  28. lock.lock();
  29. try
  30. {
  31. while (count == 0)
  32. notEmpty.await();
  33. Object x = items[takeptr];
  34. if (++takeptr == items.length)
  35. takeptr = 0;
  36. --count;
  37. notFull.signal();
  38. return x;
  39. }
  40. finally
  41. {
  42. lock.unlock();
  43. }
  44. }
  45. }

    或者通过一个实际的例子来解释Condition的用法:
    我们要打印1-9这0个数字,由A线程先打印1-3,然后由B线程打印4-6,然后再由A线程打印7-9.我们采用Condition来演示解决方法:

  1. package com.cooperation;
  2. import java.util.concurrent.locks.Condition;
  3. import java.util.concurrent.locks.Lock;
  4. import java.util.concurrent.locks.ReentrantLock;
  5. public class TestCondition
  6. {
  7. private static int value = 1;
  8. private Lock lock = new ReentrantLock();
  9. private Condition Condition456 = lock.newCondition();
  10. private Condition Condition789 = lock.newCondition();
  11. class ThreadA implements Runnable{
  12. @Override
  13. public void run()
  14. {
  15. try
  16. {
  17. lock.lock();
  18. System.out.println("首先输出1-3");
  19. while(value<=3)
  20. {
  21. System.out.println(value++);
  22. }
  23. Condition456.signal();
  24. }
  25. finally
  26. {
  27. lock.unlock();
  28. }
  29. try
  30. {
  31. lock.lock();
  32. while(value<=6)
  33. {
  34. Condition789.await();
  35. }
  36. System.out.println("输出7-9");
  37. while(value<=9)
  38. {
  39. System.out.println(value++);
  40. }
  41. }
  42. catch (InterruptedException e)
  43. {
  44. e.printStackTrace();
  45. }
  46. finally
  47. {
  48. lock.unlock();
  49. }
  50. }
  51. }
  52. class ThreadB implements Runnable{
  53. @Override
  54. public void run()
  55. {
  56. try
  57. {
  58. lock.lock();
  59. while(value<=3)
  60. {
  61. Condition456.await();
  62. }
  63. }
  64. catch (InterruptedException e)
  65. {
  66. e.printStackTrace();
  67. }
  68. finally{
  69. lock.unlock();
  70. }
  71. try{
  72. lock.lock();
  73. System.out.println("输出4-6");
  74. while(value<=6)
  75. {
  76. System.out.println(value++);
  77. }
  78. Condition789.signal();
  79. }
  80. finally
  81. {
  82. lock.unlock();
  83. }
  84. }
  85. }
  86. public static void main(String[] args)
  87. {
  88. TestCondition test = new TestCondition();
  89. Thread threadA = new Thread(test.new ThreadA());
  90. Thread threadB = new Thread(test.new ThreadB());
  91. threadA.start();
  92. threadB.start();
  93. }
  94. }

输出结果:

 

 

首先输出1-3
1
2
3
输出4-6
4
5
6
输出7-9
7
8
9

    如果需要采用Object方法的wait,notify,notifyAll方法实现这个实例可以参考:http://outofmemory.cn/java/java.util.concurrent/thread-sync-with-object-wait-notify-notifyAll
    同样可以扩张,ThreadA打印123,ThreadB打印456,再让ThreadA打印789,最后然ThreadB打印10 11 12.如下:

  1. package com.cooperation;
  2. import java.util.concurrent.locks.Condition;
  3. import java.util.concurrent.locks.Lock;
  4. import java.util.concurrent.locks.ReentrantLock;
  5. public class TestCondition
  6. {
  7. private static int value = 1;
  8. private Lock lock = new ReentrantLock();
  9. private Condition Condition456 = lock.newCondition();
  10. private Condition Condition789 = lock.newCondition();
  11. private Condition Condition101112 = lock.newCondition();
  12. class ThreadA implements Runnable{
  13. @Override
  14. public void run()
  15. {
  16. try
  17. {
  18. lock.lock();
  19. System.out.println("首先输出1-3");
  20. while(value<=3)
  21. {
  22. System.out.println(value++);
  23. }
  24. Condition456.signal();
  25. }
  26. finally
  27. {
  28. lock.unlock();
  29. }
  30. try
  31. {
  32. lock.lock();
  33. while(value<=6)
  34. {
  35. Condition789.await();
  36. }
  37. System.out.println("输出7-9");
  38. while(value<=9)
  39. {
  40. System.out.println(value++);
  41. }
  42. Condition101112.signal();
  43. }
  44. catch (InterruptedException e)
  45. {
  46. e.printStackTrace();
  47. }
  48. finally
  49. {
  50. lock.unlock();
  51. }
  52. }
  53. }
  54. class ThreadB implements Runnable{
  55. @Override
  56. public void run()
  57. {
  58. try
  59. {
  60. lock.lock();
  61. while(value<=3)
  62. {
  63. Condition456.await();
  64. }
  65. }
  66. catch (InterruptedException e)
  67. {
  68. e.printStackTrace();
  69. }
  70. finally{
  71. lock.unlock();
  72. }
  73. try{
  74. lock.lock();
  75. System.out.println("输出4-6");
  76. while(value<=6)
  77. {
  78. System.out.println(value++);
  79. }
  80. Condition789.signal();
  81. }
  82. finally
  83. {
  84. lock.unlock();
  85. }
  86. try
  87. {
  88. lock.lock();
  89. while(value<=9)
  90. {
  91. Condition101112.await();
  92. }
  93. }
  94. catch (InterruptedException e)
  95. {
  96. e.printStackTrace();
  97. }
  98. finally{
  99. lock.unlock();
  100. }
  101. try{
  102. lock.lock();
  103. System.out.println("输出10-12");
  104. while(value<=12)
  105. {
  106. System.out.println(value++);
  107. }
  108. }
  109. finally
  110. {
  111. lock.unlock();
  112. }
  113. }
  114. }
  115. public static void main(String[] args)
  116. {
  117. TestCondition test = new TestCondition();
  118. Thread threadA = new Thread(test.new ThreadA());
  119. Thread threadB = new Thread(test.new ThreadB());
  120. threadA.start();
  121. threadB.start();
  122. }
  123. }

输出结果:

 

 

首先输出1-3
1
2
3
输出4-6
4
5
6
输出7-9
7
8
9
输出10-12
10
11
12

欢迎跳转到本文的原文链接:https://honeypps.com/java/java-multi-thread-of-condition/

 

欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载