多线程之线程通信条件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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值