Condition 实现三个线程 顺序执行


   有两个子线程(线程2,线程3)和主线程,实现以下操作:
   线程2先执行10次,然后 线程3执行20次,再由主线程执行30次,然后再让线程2执行10次,再让线程3执行20次…依次类推,执行10次。


程序如下:

package test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ConditionTest {

	public static void main(String[] args) {
		final Bussiness bussiness = new Bussiness();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				for(int i=0;i<10;i++){
					bussiness.sub2(i);
				}
				
			}
		}).start();
		new Thread(new Runnable() {
			 @Override
			public void run() {
				 for(int i=0;i<10;i++){
						bussiness.sub3(i);
					}
			}
		}).start();
		
		for(int i=0;i<10;i++){
			bussiness.sub1(i);
		}
	}
	
	
}
class Bussiness{
	 Lock lock=new ReentrantLock();
        /* 此处使用了三个Condition ,
         原因:如果只使用一个Condition时,假如 线程 1 和 线程 2 都处于 await状态 的时候, 线程 3在signal()时 会随机唤醒 线程1,线程2中的一个,达不到顺序执行的要求*/
	 Condition condition1=lock.newCondition();
	 Condition condition2=lock.newCondition();
	 Condition condition3=lock.newCondition();
	private int num=2;//用于表示正在执行的线程编码
	public void sub2(int i){
		lock.lock();
		try{
			while(num != 2){
				try{
					condition2.await();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			for(int j=0;j<10;j++){
				System.out.println("sub2 thread sequence of "+j+" loop of "+i);
			}
			num=3;
			condition3.signal();
		}finally{
			lock.unlock();
		}
	}
	
	public void sub3(int i){
		lock.lock();
		
		try{
			while(num != 3){
				try{
					condition3.await();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			for(int j=0;j<20;j++){
				System.out.println("sub3 thread sequence of "+j+" loop of "+i);
			}
			num=1;
			condition1.signal();
		}finally {
			lock.unlock();
		}
	}
	
	public void sub1(int i){
		lock.lock();
		try{
			while(num != 1){
				try {
					condition1.await();
				} catch (InterruptedException e) {
					
					e.printStackTrace();
				}
			}
			
			for(int j=0;j<30;j++){
				System.out.println("sub1 thread sequence of "+j+" loop of "+i);
			}
			num=2;
			condition2.signal();
		}finally{
			lock.unlock();
		}
	}
}



结果如下:

sub2 thread sequence of 0 loop of 0
sub2 thread sequence of 1 loop of 0
sub2 thread sequence of 2 loop of 0
sub2 thread sequence of 3 loop of 0
sub2 thread sequence of 4 loop of 0
sub2 thread sequence of 5 loop of 0
sub2 thread sequence of 6 loop of 0
sub2 thread sequence of 7 loop of 0
sub2 thread sequence of 8 loop of 0
sub2 thread sequence of 9 loop of 0
sub3 thread sequence of 0 loop of 0
sub3 thread sequence of 1 loop of 0
sub3 thread sequence of 2 loop of 0
sub3 thread sequence of 3 loop of 0
sub3 thread sequence of 4 loop of 0
sub3 thread sequence of 5 loop of 0
sub3 thread sequence of 6 loop of 0
sub3 thread sequence of 7 loop of 0
sub3 thread sequence of 8 loop of 0
sub3 thread sequence of 9 loop of 0
sub3 thread sequence of 10 loop of 0
sub3 thread sequence of 11 loop of 0
sub3 thread sequence of 12 loop of 0
sub3 thread sequence of 13 loop of 0
sub3 thread sequence of 14 loop of 0
sub3 thread sequence of 15 loop of 0
sub3 thread sequence of 16 loop of 0
sub3 thread sequence of 17 loop of 0
sub3 thread sequence of 18 loop of 0
sub3 thread sequence of 19 loop of 0
sub1 thread sequence of 0 loop of 0
sub1 thread sequence of 1 loop of 0
sub1 thread sequence of 2 loop of 0
sub1 thread sequence of 3 loop of 0
sub1 thread sequence of 4 loop of 0
sub1 thread sequence of 5 loop of 0
sub1 thread sequence of 6 loop of 0
sub1 thread sequence of 7 loop of 0

……
太多了,复制了一部分。

猜你喜欢

转载自920198034.iteye.com/blog/2404375