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



原文地址:https://www.cnblogs.com/gcczhongduan/p/4243283.html