标签:线程 通信 lock condition thread
接上一篇,实现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
标签:线程 通信 lock condition thread
原文地址:http://blog.csdn.net/fengshizty/article/details/40948705