Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 477|回复: 0

关于condition的问题,请大佬们指教

[复制链接]

该用户从未签到

发表于 2015-6-4 10:27:49 | 显示全部楼层 |阅读模式
3Java金币
  • 有4条线程,老大,老二,老三,老四,最后实现的效果是:老大执行10次,接着老二再执行20次,接着老三再执行30次,接着老四再执行40次,再循环回去,进行10遍


  • package cn.test;  
  •   
  • import java.util.concurrent.locks.Condition;  
  • import java.util.concurrent.locks.Lock;  
  • import java.util.concurrent.locks.ReentrantLock;  
  •   
  • public class MultiConditionsTest {  
  •   
  •     public static void main(String[] args) {  
  •          
  •         final Task task = new Task();  
  •         // 启动四个线程,分别执行任务类中的4个任务!  
  •         new Thread(new Runnable(){  
  •             @Override  
  •             public void run() {  
  •                 for(int j=0;j<10;j++){  
  •                     task.output1();  
  •                 }  
  •             }}, "First Thread").start();  
  •         new Thread(new Runnable(){  
  •             @Override  
  •             public void run() {  
  •                 for(int j=0;j<10;j++){  
  •                     task.output2();  
  •                 }  
  •             }}, "Second Thread").start();  
  •         new Thread(new Runnable(){  
  •             @Override  
  •             public void run() {  
  •                 for(int j=0;j<10;j++){  
  •                     task.output3();  
  •                 }  
  •             }}, "Third Thread").start();  
  •         new Thread(new Runnable(){  
  •             @Override  
  •             public void run() {  
  •                 for(int j=0;j<10;j++){  
  •                     task.output4();  
  •                 }  
  •             }}, "Forth Thread").start();  
  •     }  
  •   
  •     private static class Task {  
  •   
  •         private int ctrlOrder = 0;  
  •         // 创建一个互斥控制的锁  
  •         private Lock lock = new ReentrantLock();  
  •         // 调用锁的newCondtion方法,得到一个Condtion对象!  
  •         private Condition op1Condition = lock.newCondition();  
  •         private Condition op2Condition = lock.newCondition();  
  •         private Condition op3Condition = lock.newCondition();  
  •         private Condition op4Condition = lock.newCondition();  
  •   
  •         public void output1() {  
  •   
  •             lock.lock();  
  •             try {  
  •   
  •                 // 使用Condition也会产生假醒现象!所以此处要用while循环进行判断!  
  •                 while (ctrlOrder % 4 != 0) {  
  •   
  •                     // 调用Condtion对象的await方法,进行等待  
  •                     op1Condition.await();  
  •                 }  
  •                 // 执行相应的业务逻辑  
  •                 for (int i = 0; i < 10; i++) {  
  •   
  •                     System.out.println(Thread.currentThread().getName()  
  •                             + " out put " + i);  
  •                 }  
  •                 ctrlOrder++;  
  •                 // 通过调用特定的Condition的signal来唤醒在该Condition对象上等待的线程!精确唤醒!  
  •                 op2Condition.signal();  
  •             } catch (InterruptedException e) {  
  •                 e.printStackTrace();  
  •             } finally {  
  •   
  •                 lock.unlock();  
  •             }  
  •         }  
  •   
  •         public void output2() {  
  •   
  •             lock.lock();  
  •             try {  
  •   
  •                 // 使用Condition也会产生假醒现象!所以此处要用while循环进行判断!  
  •                 while (ctrlOrder % 4 != 1) {  
  •   
  •                     // 调用Condtion对象的await方法,进行等待  
  •                     op2Condition.await();  
  •                 }  
  •                 // 执行相应的业务逻辑  
  •                 for (int i = 0; i < 20; i++) {  
  •   
  •                     System.out.println(Thread.currentThread().getName()  
  •                             + " out put " + i);  
  •                 }  
  •                 ctrlOrder++;  
  •                 // 通过调用特定的Condition的signal来唤醒在该Condition对象上等待的线程!精确唤醒!  
  •                 op3Condition.signal();  
  •             } catch (InterruptedException e) {  
  •                 e.printStackTrace();  
  •             } finally {  
  •   
  •                 lock.unlock();  
  •             }  
  •         }  
  •   
  •         public void output3() {  
  •   
  •             lock.lock();  
  •             try {  
  •   
  •                 // 使用Condition也会产生假醒现象!所以此处要用while循环进行判断!  
  •                 while (ctrlOrder % 4 != 2) {  
  •   
  •                     // 调用Condtion对象的await方法,进行等待  
  •                     op3Condition.await();  
  •                 }  
  •                 // 执行相应的业务逻辑  
  •                 for (int i = 0; i < 30; i++) {  
  •   
  •                     System.out.println(Thread.currentThread().getName()  
  •                             + " out put " + i);  
  •                 }  
  •                 ctrlOrder++;  
  •                 // 通过调用特定的Condition的signal来唤醒在该Condition对象上等待的线程!精确唤醒!  
  •                 op4Condition.signal();  
  •             } catch (InterruptedException e) {  
  •                 e.printStackTrace();  
  •             } finally {  
  •   
  •                 lock.unlock();  
  •             }  
  •         }  
  •   
  •         public void output4() {  
  •   
  •             lock.lock();  
  •             try {  
  •   
  •                 // 使用Condition也会产生假醒现象!所以此处要用while循环进行判断!  
  •                 while (ctrlOrder % 4 != 3) {  
  •   
  •                     // 调用Condtion对象的await方法,进行等待  
  •                     op4Condition.await();  
  •                 }  
  •                 // 执行相应的业务逻辑  
  •                 for (int i = 0; i < 40; i++) {  
  •   
  •                     System.out.println(Thread.currentThread().getName()  
  •                             + " out put " + i);  
  •                 }  
  •                 ctrlOrder++;  
  •                 // 通过调用特定的Condition的signal来唤醒在该Condition对象上等待的线程!精确唤醒!  
  •                 op1Condition.signal();  
  •             } catch (InterruptedException e) {  
  •                 e.printStackTrace();  
  •             } finally {  
  •   
  •                 lock.unlock();  
  •             }  
  •         }  
  •   
  •     }  
  •   
  • }  






在代码中只是生成了四个Condition对象。比如在output4()函数中op4Condition.await()是指让哪个线程任务等待?如果是output4(),那么为什么是output4而不是output1?
同样的op1Condition.signal(); 为什么是唤醒线程任务output1()?
线程任务和condition在程序代码中没有具体的代码来建立关系,他们是如何关联的呢?

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-4-25 20:10 , Processed in 0.458204 second(s), 45 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表