博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程之线程通信条件Condition二
阅读量:5943 次
发布时间:2019-06-19

本文共 3579 字,大约阅读时间需要 11 分钟。

接上一篇,实现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 1sub2 thread sequence of 2, task is 1sub1 thread sequence of 1, task is 1sub3 thread sequence of 1, task is 1sub3 thread sequence of 2, task is 1sub3 thread sequence of 3, task is 1sub2 thread sequence of 1, task is 2sub2 thread sequence of 2, task is 2sub1 thread sequence of 1, task is 2sub3 thread sequence of 1, task is 2sub3 thread sequence of 2, task is 2sub3 thread sequence of 3, task is 2sub2 thread sequence of 1, task is 3sub2 thread sequence of 2, task is 3sub1 thread sequence of 1, task is 3sub3 thread sequence of 1, task is 3sub3 thread sequence of 2, task is 3sub3 thread sequence of 3, task is 3sub2 thread sequence of 1, task is 4sub2 thread sequence of 2, task is 4sub1 thread sequence of 1, task is 4sub3 thread sequence of 1, task is 4sub3 thread sequence of 2, task is 4sub3 thread sequence of 3, task is 4sub2 thread sequence of 1, task is 5sub2 thread sequence of 2, task is 5sub1 thread sequence of 1, task is 5sub3 thread sequence of 1, task is 5sub3 thread sequence of 2, task is 5sub3 thread sequence of 3, task is 5

转载地址:http://dzwxx.baihongyu.com/

你可能感兴趣的文章
接口由40秒到200ms优化记录
查看>>
java 视频播放 多人及时弹幕技术 代码生成器 websocket springmvc mybatis SSM
查看>>
Activiti6.0,spring5,SSM,工作流引擎,OA
查看>>
第十三章:SpringCloud Config Client的配置
查看>>
使用 GPUImage 实现一个简单相机
查看>>
CoinWhiteBook:区块链在慈善事业中的应用
查看>>
Mac上基于Github搭建Hexo博客
查看>>
阿里云服务器ECS开放8080端口
查看>>
Spring中实现监听的方法
查看>>
使用Tooltip会出现一个问题,如果行上出现复选框
查看>>
11.03T1 DP
查看>>
P2924 [USACO08DEC]大栅栏Largest Fence
查看>>
jQuery操作table tr td
查看>>
工作总结:MFC自写排序算法(升序)
查看>>
螺旋队列问题之二
查看>>
扩展运算符和解构赋值的理解
查看>>
手机H5显示一像素的细线
查看>>
Menu 菜单栏
查看>>
Integer跟int的区别(备份回忆)
查看>>
集合解析
查看>>