java 多个线程之间同步通信



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

/**
 *  Lock 类似  synchronize  (让多个线程 执行任务的时候 相互不干扰,不被打断  :互斥)  
 *	Condition  (让多个线程之间 进行 交流   :通信)
 *
 *  例子 : 老大 --> 老二  -->老三     --> 老大 --> 老二  -->老三    
 */
public class ThreeConditionCommunication2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		final Business bus=new ThreeConditionCommunication2().new Business();
		
		new Thread(
				new Runnable(){

					@Override
					public void run() {
						for (int j = 0; j < 5; j++) {
							bus.sub2(j);
						}
					}
				}
		).start();
			
		new Thread(
				new Runnable(){

					@Override
					public void run() {
						for (int j = 0; j < 5; j++) {
							bus.sub3(j);
						}
					}
				}
		).start();
		
		for (int j = 0; j < 5; j++) {
			bus.sub1(j);
		}
			
		
	}

	//主要是这里不同 
	class Business{
		Lock lock=new ReentrantLock();
		Condition con1=lock.newCondition();  // 1 - 2 通信
		Condition con2=lock.newCondition();  // 2 - 3 通信 
		
		int step=1;
		public void sub1(int i){
			lock.lock();
			try {
				while(step!=1)
					con1.await();
				
				for (int j = 0; j < 100; j++) {
					System.out.println("main thread sequence of "+j+" loop of"+i);
				}
				step=2;
				con1.signal();
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				lock.unlock();
			}
		}
		
		public void sub2(int i){
			lock.lock();
			try {
				while(step!=2)
					con1.await();
				
				for (int j = 0; j < 10; j++) {
					System.out.println("Sub2 thread sequence of "+j+" loop of"+i);
				}
				step=3;
				con2.signal();
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				lock.unlock();
			}
		}
		
		public void sub3(int i){
			lock.lock();
			try {
				while(step!=3)
					con2.await();
				
				for (int j = 0; j < 20; j++) {
					System.out.println("Sub3 thread sequence of "+j+" loop of"+i);
				}
				step=1;
				con1.signal();
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				lock.unlock();
			}
		}
	}
}





/**
 *  Lock 类似  synchronize  (让多个线程 执行任务的时候 相互不干扰,不被打断  :互斥)  
 *	Condition  (让多个线程之间 进行 交流   :通信)
 *
 * 例子 :三个线程之间的互斥  和 通信
 */
public class ThreeConditionCommunication {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		final Business bus=new ThreeConditionCommunication().new Business();
		
		new Thread(
				new Runnable(){

					@Override
					public void run() {
						for (int j = 0; j < 5; j++) {
							bus.sub2(j);
						}
					}
				}
		).start();
			
		new Thread(
				new Runnable(){

					@Override
					public void run() {
						for (int j = 0; j < 5; j++) {
							bus.sub3(j);
						}
					}
				}
		).start();
		
		
		for (int j = 0; j < 5; j++) {
			bus.sub1(j);
		}
			
		
	}

	// 三个 Condition
	class Business{
		Lock lock=new ReentrantLock();
		Condition con1=lock.newCondition();
		Condition con2=lock.newCondition();
		Condition con3=lock.newCondition();
		int step=1;
		
		public void sub1(int i){
			lock.lock();
			try {
				while(step!=1)
					con1.await();
				
				for (int j = 0; j < 20; j++) {
					System.out.println("main thread sequence of "+j+" loop of"+i);
				}
				step=2;
				con2.signal();
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				lock.unlock();
			}
		}
		
		public void sub2(int i){
			lock.lock();
			try {
				while(step!=2)
					con2.await();
				
				for (int j = 0; j < 10; j++) {
					System.out.println("Sub2 thread sequence of "+j+" loop of"+i);
				}
				step=3;
				con3.signal();
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				lock.unlock();
			}
		}
		
		public void sub3(int i){
			lock.lock();
			try {
				while(step!=3)
					con3.await();
				
				for (int j = 0; j < 20; j++) {
					System.out.println("Sub3 thread sequence of "+j+" loop of"+i);
				}
				step=1;
				con1.signal();
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				lock.unlock();
			}
		}
		
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值