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

来源:互联网 发布:linux setfacl命令 编辑:程序博客网 时间:2024/05/12 11:37
最近在学习复习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(){@Overridepublic void run() {// TODO Auto-generated method stubfor(int i=0;i<10;i++){outer.main(i);}}}).start();new Thread(new Runnable(){@Overridepublic void run() {// TODO Auto-generated method stubfor(int i=0;i<10;i++){outer.sub1(i);}}}).start();new Thread(new Runnable(){@Overridepublic void run() {// TODO Auto-generated method stubfor(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 blocke.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 blocke.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 blocke.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(){@Overridepublic void run() {// TODO Auto-generated method stubfor(int i=0;i<5;i++){outer.main(i);}}}).start();new Thread(new Runnable(){@Overridepublic void run() {// TODO Auto-generated method stubfor(int i=0;i<5;i++){outer.sub1(i);}}}).start();new Thread(new Runnable(){@Overridepublic void run() {// TODO Auto-generated method stubfor(int i=0;i<5;i++){outer.sub2(i);}}}).start();}static class Outer{staticint flag=1;public synchronized void main(int i){if(flag != 1){try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.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 blocke.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 blocke.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仅仅是一个判断语句

阅读全文
0 0
原创粉丝点击