用java传统线程方式实现多线程轮询执行问题

最近在学习复习java多线程方面的知识,看到一道多线程方面问题。主线程执行三次,然后子线程1执行三次,最后子线程2执行三次,按照这样的规则循环执行10次。一开始使用Lock和Condition两个类来实现功能。因为一个锁可以创建多个Condition,每个线程拥有一个自己的Condition,通过某个线程的Condition唤醒特定线程,很容易就能实现需求。具体代码如下:
public class ThreeConditionTest {

	
	public static void main(String[] args) {
		final Outer outer=new Outer();
		new Thread(new Runnable(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				for(int i=0;i<10;i++){
					outer.main(i);
				}
			}
		}).start();
		new Thread(new Runnable(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				for(int i=0;i<10;i++){
					outer.sub1(i);
				}
			}
		}).start();
		new Thread(new Runnable(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				for(int i=0;i<10;i++){
					outer.sub2(i);
				}
			}
		}).start();
	
	}
	
	static class Outer{
		Lock lock = new ReentrantLock();
		Condition mainCondition = lock.newCondition();
		Condition sub1Condition = lock.newCondition();
		Condition sub2Condition = lock.newCondition();
		int flag=1;
		
		public void main(int i){
			lock.lock();
			try{
				if(flag != 1){
					try {
						mainCondition.await();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				for(int j=0;j<3;j++){
					System.out.println("main thread "+(j+1)+" 次"+" loop of "+(i+1));
				}
				flag = 2;
				sub1Condition.signal();
			}finally{
				lock.unlock();
			}
		}
		
		public void sub1(int i){
			lock.lock();
			try{
				if(flag != 2){
					try {
						sub1Condition.await();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				for(int j=0;j<3;j++){
					System.out.println("sub1 thread "+(j+1)+" 次"+" loop of "+(i+1));
				}
				flag=3;
				sub2Condition.signal();
			}finally{
				lock.unlock();
			}
		}
		
		public void sub2(int i){
			lock.lock();
			try{
				if(flag != 3){
					try {
						sub2Condition.await();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				for(int j=0;j<3;j++){
					System.out.println("sub2 thread "+(j+1)+" 次"+" loop of "+(i+1));
				}
				flag = 1;
				mainCondition.signal();
			}finally{
				lock.unlock();
			}
		}
		
	}
}
java传统同步方法(synchronized)提供notify()和notifyAll()两个常用的多线程之间通信方式,但是这两个方法都不能唤醒某个指定的线程,只能随机唤醒一个或者全部唤醒,这就使得让多线程按照我们的要求来执行变得没那么容易,而java5的锁可以指定唤醒某个线程,这使得我们编写此类多线程轮训执行的问题变得很简单。今天复习的时候突然想试一下用传统线程实现。
代码如下:
public class ThreeWaitTest {

	public static void main(String[] args) {
		final Outer outer=new Outer();
		new Thread(new Runnable(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				for(int i=0;i<5;i++){
					outer.main(i);
				}
			}
		}).start();
		new Thread(new Runnable(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				for(int i=0;i<5;i++){
					outer.sub1(i);
				}
			}
		}).start();
		new Thread(new Runnable(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				for(int i=0;i<5;i++){
					outer.sub2(i);
				}
			}
		}).start();
	
	}
	
	static class Outer{
		
		static	int flag=1;
		public synchronized void main(int i){
			
			if(flag != 1){
					try {
						this.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				for(int j=0;j<3;j++){
					System.out.println("main thread "+(j+1)+" 次"+" loop of "+(i+1));
				}
				
				flag = 2;
				this.notifyAll();
			
		}
		
		public synchronized void sub1(int i){
			if(flag != 2){
					try {
						this.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				for(int j=0;j<3;j++){
					System.out.println("sub1 thread "+(j+1)+" 次"+" loop of "+(i+1));
				}
				flag=3;
				this.notifyAll();
		}
		
		public synchronized void sub2(int i){
				if(flag != 3){
					try {
						this.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				for(int j=0;j<3;j++){
					System.out.println("sub2 thread "+(j+1)+" 次"+" loop of "+(i+1));
				}
				flag = 1;
				this.notifyAll();
		}
		
	}
}
实现起来貌似并没有那么难,而且代码看起来完全没问题。但是执行的结果却意外的出错了,于是来回检查代码,完全找不到错误所在。苦思冥想一上午,想着是否传统线程就实现不了三个及以上的多线程之间通信,正当打算放弃的时候,突然想起了以前无意中看到的关于线程假唤醒的问题。于是把判断线程是否该进入等待状态的if语句换成了while语句。没想到最后真的成功了。最后在此提醒大家一句也是提醒一下自己。在多线程编程中,判断条件尽量用while,因为while是表达循环语句,只要不满足条件就会一直循环下去,不会发生假唤醒状况,而if仅仅是一个判断语句

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值