多线程之CountDownLatch

CountDownLatch是Jdk5包Concurrent下很实用的工具类之一,主要用来实现多个执行线程的排序,下面例子通过三种方式实现同一功能(本例子JDK5下通过)
package regbin.exa.thread.concurrent;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class CountDownLatchTest {
	public void CountDownLatchExa() throws InterruptedException {
		final CountDownLatch countDownBegin = new CountDownLatch(1);
		final CountDownLatch countDownEnd = new CountDownLatch(2);
		Thread threadA = new Thread() {
			@Override
			public void run() {
				try {
					countDownBegin.await();
					System.out.println("threadA");
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					countDownEnd.countDown();
				}
			}
		};
		Thread threadB = new Thread() {
			@Override
			public void run() {
				try {
					countDownBegin.await();
					System.out.println("threadB");
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					countDownEnd.countDown();
				}

			}
		};
		System.out.println("CountDownLatchExa-X");
		threadA.start();
		threadB.start();
		countDownBegin.countDown();
		countDownEnd.await();
		System.out.println("CountDownLatchExa-Y");
	}

	public void waitNotifyExa() throws InterruptedException {
		final Object object = new Object();
		Thread threadA = new Thread() {
			@Override
			public void run() {
				try {
					synchronized (object) {
						object.wait();
					}
					System.out.println("threadA");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
		Thread threadB = new Thread() {
			@Override
			public void run() {
				try {
					synchronized (object) {
						object.wait();
					}
					System.out.println("threadB");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			}
		};
		System.out.println("waitNotifyExa-X");
		threadA.start();
		threadB.start();
		Thread.sleep(2000);//保证通知动作在线程启动之后发出
		synchronized (object) {
			object.notifyAll();
		}
		System.out.println("waitNotifyExa-Y");
	}

	public void CurrentLockExa() throws InterruptedException {
		final Lock lock = new ReentrantLock();
		final Condition object = lock.newCondition();
		Thread threadA = new Thread() {
			@Override
			public void run() {
				lock.lock();
				try {
					object.await();
					System.out.println("threadA");
				} catch (InterruptedException e) {
				} finally {
					lock.unlock();
				}
			}
		};
		Thread threadB = new Thread() {
			@Override
			public void run() {
				lock.lock();
				try {
					object.await();
					System.out.println("threadB");
				} catch (InterruptedException e) {
				} finally {
					lock.unlock();
				}

			}
		};
		System.out.println("CurrentLockExa-X");
		threadA.start();
		threadB.start();
		Thread.sleep(2000);//保证通知动作在线程启动之后发出
		lock.lock();
		try {
			object.signalAll();
		} finally {
			lock.unlock();
		}
		System.out.println("CurrentLockExa-Y");
	}

	public static void main(String[] args) throws InterruptedException {
		CountDownLatchTest test = new CountDownLatchTest();
		test.CountDownLatchExa();
		test.waitNotifyExa();
		test.CurrentLockExa();
	}
}


你可能感兴趣的:(多线程,thread)