接上一篇,实现Condition三个条件,有这样一个应用:

1、 有三个进程,第一个进程运行1次,第二个进程运行2次,第三个进程运行3次;

2、 先运行第二个进程,然后第一个,然后第三个;

3、  依次运行5次循环。

分析:

此时若用Object的wait和notify是实现不了的,我们能够用Lock锁的Condition实现,我们须要定义三个信号条件,分别控制这三个进程。

实现例如以下:

package andy.thread.test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; /**
* @author Zhang,Tianyou
* @version 2014年11月9日 下午12:12:44
*/ public class ThreeThreadCondition { static A tasks = new A(); public static void main(String[] args) {
// 2号线程
new Thread(new Runnable() { @Override
public void run() {
for (int i = 1; i <= 5; i++) {
// 循环运行5次
tasks.sub2(i);
} }
}).start(); // 3号线程
new Thread(new Runnable() { @Override
public void run() {
for (int i = 1; i <= 5; i++) {
// 循环运行5次
tasks.sub3(i);
} }
}).start(); // 主线程取代1号线程
for (int i = 1; i <= 5; i++) {
// 循环运行5次
tasks.sub1(i);
}
} static class A {
Lock lock = new ReentrantLock();
Condition condition1 = lock.newCondition();
Condition condition2 = lock.newCondition();
Condition condition3 = lock.newCondition(); // 先运行2号线程
private int execuNum = 2; public void sub2(int i) {
lock.lock();
try {
// 若不是2 则堵塞等待
while (execuNum != 2) {
try {
condition2.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } for (int j = 1; j <= 2; j++) {
System.out.println("sub2 thread sequence of " + j
+ ", task is " + i);
} // 运行完 交给1线程
execuNum = 1;
condition1.signal(); } finally {
lock.unlock();
}
} public void sub1(int i) {
lock.lock();
try {
// 若不是1则堵塞等待
while (execuNum != 1) {
try {
condition1.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } System.out.println("sub1 thread sequence of " + 1
+ ", task is " + i); // 运行完 交给3线程
execuNum = 3;
condition3.signal(); } finally {
lock.unlock();
}
} public void sub3(int i) {
lock.lock();
try {
// 若不是2 则堵塞等待
while (execuNum != 3) {
try {
condition3.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } for (int j = 1; j <= 3; j++) {
System.out.println("sub3 thread sequence of " + j
+ ", task is " + i);
} // 运行完 交给1线程
execuNum = 2;
condition2.signal(); } finally {
lock.unlock();
}
} } }

执行结果例如以下:

sub2 thread sequence of 1, task is 1
sub2 thread sequence of 2, task is 1
sub1 thread sequence of 1, task is 1
sub3 thread sequence of 1, task is 1
sub3 thread sequence of 2, task is 1
sub3 thread sequence of 3, task is 1
sub2 thread sequence of 1, task is 2
sub2 thread sequence of 2, task is 2
sub1 thread sequence of 1, task is 2
sub3 thread sequence of 1, task is 2
sub3 thread sequence of 2, task is 2
sub3 thread sequence of 3, task is 2
sub2 thread sequence of 1, task is 3
sub2 thread sequence of 2, task is 3
sub1 thread sequence of 1, task is 3
sub3 thread sequence of 1, task is 3
sub3 thread sequence of 2, task is 3
sub3 thread sequence of 3, task is 3
sub2 thread sequence of 1, task is 4
sub2 thread sequence of 2, task is 4
sub1 thread sequence of 1, task is 4
sub3 thread sequence of 1, task is 4
sub3 thread sequence of 2, task is 4
sub3 thread sequence of 3, task is 4
sub2 thread sequence of 1, task is 5
sub2 thread sequence of 2, task is 5
sub1 thread sequence of 1, task is 5
sub3 thread sequence of 1, task is 5
sub3 thread sequence of 2, task is 5
sub3 thread sequence of 3, task is 5

多线程之线程通信条件Condition二的更多相关文章

  1. 多线程之线程通信条件Condition

    Condition是Locks锁下的还有一种线程通信之间唤醒.堵塞的实现.它以下的await,和signal可以实现Object下的wait,notify和notifyAll的所有功能,除此之外改监视 ...

  2. Java之多线程开发时多条件Condition接口的使用

    转:http://blog.csdn.net/a352193394/article/details/39454157 我们在多线程开发中,可能会出现这种情况.就是一个线程需要另外一个线程满足某某条件才 ...

  3. java 多线程:线程通信-等待通知机制wait和notify方法;(同步代码块synchronized和while循环相互嵌套的差异);管道通信:PipedInputStream;PipedOutputStream;PipedWriter; PipedReader

    1.等待通知机制: 等待通知机制的原理和厨师与服务员的关系很相似: 1,厨师做完一道菜的时间不确定,所以厨师将菜品放到"菜品传递台"上的时间不确定 2,服务员什么时候可以取到菜,必 ...

  4. Scala学习笔记之Actor多线程与线程通信的简单例子

    题目:通过子线程读取每个文件,并统计单词数,将单词数返回给主线程相加得出总单词数 package review import scala.actors.{Actor, Future} import s ...

  5. java 多线程 day04 线程通信

    package com.czbk.thread; /** * Created by chengtao on 17/12/3. * 需求: 子线程先运行10次,然后主线程运行 100次,依次运行50次 ...

  6. java多线程(线程通信-等待换新机制-代码优化)

    等待唤醒机制涉及方法: wait():让线程处于冻结状态,被wait的线程会被存储到线程池中. noticfy():唤醒同一个线程池中一个线程(任意也可能是当前wait的线程) notifyAll() ...

  7. 6.显示锁Lock 和 线程通信Condition

    显示锁 Lock 一.用于解决多线程 安全问题的方式: synchronized:   1.同步代码块      2.同步方法 jdk1.5 后:第三种:同步锁Lock  (注意:同步(synchro ...

  8. {Python之线程} 一 背景知识 二 线程与进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 七 python与线程 八 Threading模块 九 锁 十 信号量 十一 事件Event 十二 条件Condition(了解) 十三 定时器

    Python之线程 线程 本节目录 一 背景知识 二 线程与进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 七 python与线程 八 Thr ...

  9. 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题

    调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...

随机推荐

  1. ArcGIS Api For Flex 动态画点和线

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  2. 3.SOAP和WSDL的一些必要知识

    转自:https://www.cnblogs.com/JeffreySun/archive/2009/12/14/1623766.html SOAP和WSDL对Web Service.WCF进行深入了 ...

  3. 企业网管软件实战之看视频学装Cisco Works 2000

    650) this.width=650;" border="0" alt="125711349.jpg" src="http://img1. ...

  4. Wiz+360云盘,让你的知识库井井有条

    用了wiz快两年了,一些同事看到我在找资料时打开wiz,总会好奇的问这是什么,想到还有很多同仁在用文件夹管理知识库,于是想分享一下我的管理方法.(PS:鄙人愚见,如有高见,望指教) Wiz为知笔记下载 ...

  5. 父子margin塌陷

    1.使用padding 2.给父级使用border 3.给父级添加属性 overflow:hidden 4.浮动 5.定位{absolute,fixed} 6.伪元素代码 .parent:before ...

  6. 【2017 Multi-University Training Contest - Team 7】Just do it

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6129 [Description] 设定b[i]=a[1]^a[2]^a[3]^------a[i] ...

  7. leetCode解题报告5道题(十)

    题目一:Valid Number Validate if a given string is numeric. Some examples: "0" => true &quo ...

  8. 看<Asp.net夜话>随笔(2013-10-13)

    1.Asp.net内置对象 1.1Request对象 封装了客户端请求信息 1.2Response对象 代表了服务器响应对象,可以向客户端返回数据 1.3Server对象 是用于获取服务器的相关信息的 ...

  9. Numpy库进阶教程(一)求解线性方程组

    前言 Numpy是一个很强大的python科学计算库.为了机器学习的须要.想深入研究一下Numpy库的使用方法.用这个系列的博客.记录下我的学习过程. 系列: Numpy库进阶教程(二) 正在持续更新 ...

  10. 1.5 Python基础知识 - while循环

    在我们生活中有很多反复要做的事情,或者动作,我们称之为循环.在开发程序中也会有循环的事情要去做,就是需要反复的去执行某个代码,或者反复进行某种演算,直到达到某种条件的时候才会停止.在Python中我们 ...