首页 > 代码库 > 多线程之线程通信条件Condition二
多线程之线程通信条件Condition二
接上一篇,实现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二
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。