线程之间除了同步互斥,还要考虑通信。在Java5之前我们的通信方式为:wait 和 notify。
那么Condition的优势是支持多路等待,就是我可以定义多个Condition,每个condition控制线程的一条执行通路。传统方式只能是一路等待。我们可以先分析下Java5 Api中的缓冲队列的实现:
假定有一个绑定的缓冲区,它支持 put 和 take 方法。如果试图在空的缓冲区上执行take 操作,则在某一个项变得可用之前,线程将一直阻塞;如果试图在满的缓冲区上执行put 操作,则在有空间变得可用之前,线程将一直阻塞。我们喜欢在单独的等待 set 中保存put 线程和take 线程,这样就可以在缓冲区中的项或空间变得可用时利用最佳规划,一次只通知一个线程。可以使用两个Condition
实例来做到这一点。
- class BoundedBuffer {
- final Lock lock = new ReentrantLock();//实例化一个锁对象
- final Condition notFull = lock.newCondition(); //实例化两个condition
- final Condition notEmpty = lock.newCondition();
- final Object[] items = new Object[100];//初始化一个长度为100的队列
- int putptr, takeptr, count;
- public void put(Object x) throws InterruptedException {
- lock.lock();//获取锁
- try {
- while (count == items.length)
- notFull.await();//当计数器count等于队列的长度时,不能在插入,因此等待
- items[putptr] = x; //将对象放入putptr索引处
- if (++putptr == items.length) putptr = 0;//当索引长度等于队列长度时,将putptr置为0
- //原因是,不能越界插入
- ++count;//没放入一个对象就将计数器加1
- notEmpty.signal();//一旦插入就唤醒取数据线程
- } finally {
- lock.unlock();//最后释放锁
- }
- }
- public Object take() throws InterruptedException {
- lock.lock();//获取锁
- try {
- while (count == 0) //如果计数器等于0那么等待
- notEmpty.await();
- Object x = items[takeptr]; //取得takeptr索引处对象
- if (++takeptr == items.length) takeptr = 0;//当takeptr达到队列长度时,从零开始取
- --count;//每取一个讲计数器减1
- notFull.signal();//枚取走一个就唤醒存线程
- return x;
- } finally {
- lock.unlock();//释放锁
- }
- }
- }
下面还有一个例子:
启动main,sub2,sub3三个线程,sub2运行完后sub3运行,sub3运行完成后main运行,main运行完成后sub2运行,如此循环往复50次。实现代码如下:
- import java.util.concurrent.locks.Condition;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
- public class ConditionCommunication {
- /**
- * @param args
- */
- public static void main(String[] args) {
- final Business business = new Business();
- new Thread(new Runnable(){
- public void run() {
- for(int i=0; i<50; i++){
- business.sub2(i);
- }
- }
- }).start();
- new Thread(new Runnable(){
- public void run() {
- for(int i=0; i<50; i++){
- business.sub3(i);
- }
- }
- }).start();
- new Thread(new Runnable(){
- public void run() {
- for(int i=0; i<50; i++){
- business.main(i);
- }
- }
- }).start();
- }
- static class Business{
- private int shouldSub = 1;
- private Lock lock = new ReentrantLock();
- Condition condition1 = lock.newCondition();
- Condition condition2 = lock.newCondition();
- Condition condition3 = lock.newCondition();
- public void sub2(int i){
- try{
- lock.lock();
- while(shouldSub != 2){
- try {
- // this.wait();
- condition2.await();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- for(int j=1; j<=10; j++){
- System.out.println("sub2 thread sequence is " + j + " loop of " + i);
- }
- shouldSub = 3;
- // this.notify();
- condition3.signal();
- }finally{
- lock.unlock();
- }
- }
- public void sub3(int i){
- try{
- lock.lock();
- while(shouldSub != 3){
- try {
- // this.wait();
- condition3.await();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- for(int j=1; j<=20; j++){
- System.out.println("sub3 thread sequence is " + j + " loop of " + i);
- }
- shouldSub = 1;
- // this.notify();
- condition1.signal();
- }finally{
- lock.unlock();
- }
- }
- public void main(int i){
- try{
- lock.lock();
- while(shouldSub != 1){
- try {
- // this.wait();
- condition1.await();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- for(int j=1; j<=100; j++){
- System.out.println("main thread sequence is " + j + " loop of " + i);
- }
- shouldSub = 2;
- // this.notify();
- condition2.signal();
- }finally{
- lock.unlock();
- }
- }
- }
- }