Condition implements sequential execution of three threads


   There are two sub-threads (thread 2, thread 3) and the main thread, which implements the following operations:
   thread 2 executes 10 times first, then thread 3 executes 20 times, then the main thread executes 30 times, and then let thread 2 execute 10 times, Then let thread 3 execute 20 times...and so on, execute 10 times.


The procedure is as follows:

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();
        /* Three Conditions are used here,
         Reason: If only one Condition is used, if thread 1 and thread 2 are both in the await state, thread 3 will randomly wake up thread 1 and one of thread 2 during signal(), which cannot meet the requirements of sequential execution*/
	 Condition condition1=lock.newCondition();
	 Condition condition2=lock.newCondition();
	 Condition condition3=lock.newCondition();
	private int num=2;//Used to indicate the thread code being executed
	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

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326485133&siteId=291194637