首页 > 代码库 > 多线程之线程通信条件Condition

多线程之线程通信条件Condition

Condition是Locks锁下的还有一种线程通信之间唤醒、堵塞的实现。它以下的await,和signal可以实现Object下的wait,notify和notifyAll的所有功能,除此之外改监视器和已绑定到每一个条件,可以实现多条件的监听。Condition实质是被绑定到一个锁上,腰围特定的Lock实例获得Condition,即用 newCondition()方法。

Condition下的await()相对于Object下的wait(); 堵塞或中断之前状况,让其处于等待状态。

Condition下的Signal();相当于Object下的notify();唤醒一个等待的状态。


Condition应用:

假设有一个缓存区,大小100,它支持put和take方法。假设试图在空的缓存区上运行take操作,则在某一项变得可用之前,线程一直堵塞;假设试图在满的缓存区上运行操作,则在有空间变得可用之前,线程一直讲堵塞。我们能够使用两个条件,一个记录缓存区的满,一个记录缓存区的空。


技术分享


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日 上午11:39:30
 */

public class ThreadConditiionBoundedBuffer {

	public static void main(String[] args) {

		BoundedBuffer buffer = new ThreadConditiionBoundedBuffer().new BoundedBuffer();
		
		for (int i = 0; i < 100; i++) {
			int task = i;
			new Thread((new Runnable() {
				public void run() {
					try {
						String putString  = Thread.currentThread().getName() + task;
						buffer.put(putString);
						System.out.println(putString);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			})).start();
		}
		
        
		for (int i = 0; i < 100; i++) {
			new Thread((new Runnable() {
				public void run() {
                   try {
					System.out.println( Thread.currentThread() + "take " + buffer.take());
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}
			})).start();
		}
	}

	class BoundedBuffer {
		// 定义锁
		Lock lock = new ReentrantLock();
		Condition notFull = lock.newCondition(); // 没有满
		Condition notEmpty = lock.newCondition(); // 不为空

		Object[] items = new Object[100];

		int putptr, takeptr, count;

		public void put(Object x) throws InterruptedException {
			lock.lock();
			try {

				// 假设已满 堵塞等待
				while (count == items.length)
					notFull.await();
				// 假设未满运行以下 加入
				items[putptr] = x;
				if (++putptr == items.length)
					putptr = 0;
				++count;
				// 唤醒读取缓冲的线程
				notEmpty.signal();

			} finally {
				lock.unlock();
			}
		}

		public Object take() throws InterruptedException {
			lock.lock();
			try {
				// 假设为空 等待
				while (count == 0)
					notEmpty.await();
				// 不为空按顺序去
				Object x = items[takeptr];
				if (++takeptr == items.length)
					takeptr = 0;
				--count;
				notFull.signal();
				return x;

			} finally {
				lock.unlock();
			}
		}
	}

}

运行效果例如以下:

Thread-00
Thread-22
Thread-11
Thread-1414
Thread-1212
Thread-1010
Thread-88
Thread-66
Thread-44
Thread-1616
Thread-1818
Thread-77
Thread-2222
Thread-2020
Thread-55
Thread-33
Thread-2424
Thread-2626
Thread-99
Thread-2828
Thread-1313
Thread-3030
Thread-1717
Thread-1111
Thread-3232
Thread-1919
Thread-1515
Thread-2121
Thread-2323
Thread-2525
Thread-3434
Thread-3131
Thread-2727
Thread-3333
Thread-2929
Thread-3535
Thread-3636
Thread-3737
Thread-3838
Thread-4040
Thread-3939
Thread-4242
Thread-6464
Thread-6666
Thread-6262
Thread-6060
Thread-5858
Thread-4747
Thread-5656
Thread-4545
Thread-5050
Thread-4848
Thread-5252
Thread-4646
Thread-5454
Thread-4444
Thread-4343
Thread-6868
Thread-4141
Thread-7070
Thread-7272
Thread-7474
Thread-7676
Thread-4949
Thread-7878
Thread-6969
Thread-8080
Thread-7171
Thread-8282
Thread-7373
Thread-7575
Thread-8484
Thread-8686
Thread-7777
Thread-8888
Thread-8181
Thread-9090
Thread-8989
Thread-8383
Thread-7979
Thread-8787
Thread-8585
Thread-6767
Thread-9292
Thread-9191
Thread-6565
Thread-9494
Thread-6363
Thread-6161
Thread-9696
Thread-9898
Thread-5959
Thread-5757
Thread-5555
Thread-5353
Thread-9393
Thread-9595
Thread[Thread-100,5,main]take Thread-00
Thread-9999
Thread[Thread-101,5,main]take Thread-22
Thread[Thread-102,5,main]take Thread-11
Thread-9797
Thread[Thread-104,5,main]take Thread-44
Thread[Thread-106,5,main]take Thread-66
Thread[Thread-108,5,main]take Thread-88
Thread[Thread-110,5,main]take Thread-1010
Thread-5151
Thread[Thread-112,5,main]take Thread-1212
Thread[Thread-114,5,main]take Thread-1616
Thread[Thread-116,5,main]take Thread-1818
Thread[Thread-118,5,main]take Thread-77
Thread[Thread-120,5,main]take Thread-2020
Thread[Thread-122,5,main]take Thread-2222
Thread[Thread-124,5,main]take Thread-55
Thread[Thread-103,5,main]take Thread-1414
Thread[Thread-126,5,main]take Thread-33
Thread[Thread-128,5,main]take Thread-2424
Thread[Thread-105,5,main]take Thread-2626
Thread[Thread-130,5,main]take Thread-99
Thread[Thread-107,5,main]take Thread-2828
Thread[Thread-132,5,main]take Thread-1313
Thread[Thread-109,5,main]take Thread-3030
Thread[Thread-134,5,main]take Thread-1515
Thread[Thread-111,5,main]take Thread-1111
Thread[Thread-136,5,main]take Thread-1717
Thread[Thread-113,5,main]take Thread-3232
Thread[Thread-138,5,main]take Thread-1919
Thread[Thread-140,5,main]take Thread-2121
Thread[Thread-115,5,main]take Thread-2323
Thread[Thread-142,5,main]take Thread-2525
Thread[Thread-117,5,main]take Thread-3434
Thread[Thread-144,5,main]take Thread-3131
Thread[Thread-119,5,main]take Thread-2727
Thread[Thread-146,5,main]take Thread-2929
Thread[Thread-148,5,main]take Thread-3333
Thread[Thread-121,5,main]take Thread-3535
Thread[Thread-150,5,main]take Thread-3636
Thread[Thread-152,5,main]take Thread-3737
Thread[Thread-123,5,main]take Thread-4040
Thread[Thread-125,5,main]take Thread-3838
Thread[Thread-127,5,main]take Thread-3939
Thread[Thread-154,5,main]take Thread-4242
Thread[Thread-129,5,main]take Thread-6666
Thread[Thread-156,5,main]take Thread-6464
Thread[Thread-131,5,main]take Thread-6262
Thread[Thread-158,5,main]take Thread-6060
Thread[Thread-160,5,main]take Thread-5656
Thread[Thread-137,5,main]take Thread-4545
Thread[Thread-133,5,main]take Thread-5858
Thread[Thread-135,5,main]take Thread-4747
Thread[Thread-162,5,main]take Thread-5454
Thread[Thread-139,5,main]take Thread-5252
Thread[Thread-164,5,main]take Thread-5050
Thread[Thread-143,5,main]take Thread-4646
Thread[Thread-141,5,main]take Thread-4848
Thread[Thread-145,5,main]take Thread-4444
Thread[Thread-166,5,main]take Thread-4343
Thread[Thread-147,5,main]take Thread-6868
Thread[Thread-168,5,main]take Thread-4141
Thread[Thread-157,5,main]take Thread-7070
Thread[Thread-151,5,main]take Thread-7272
Thread[Thread-170,5,main]take Thread-7474
Thread[Thread-155,5,main]take Thread-7676
Thread[Thread-153,5,main]take Thread-4949
Thread[Thread-172,5,main]take Thread-7878
Thread[Thread-149,5,main]take Thread-6969
Thread[Thread-174,5,main]take Thread-8080
Thread[Thread-159,5,main]take Thread-7171
Thread[Thread-176,5,main]take Thread-8282
Thread[Thread-161,5,main]take Thread-7373
Thread[Thread-163,5,main]take Thread-7575
Thread[Thread-178,5,main]take Thread-8484
Thread[Thread-165,5,main]take Thread-8686
Thread[Thread-167,5,main]take Thread-7777
Thread[Thread-180,5,main]take Thread-8888
Thread[Thread-182,5,main]take Thread-8181
Thread[Thread-169,5,main]take Thread-9090
Thread[Thread-184,5,main]take Thread-8989
Thread[Thread-171,5,main]take Thread-8383
Thread[Thread-173,5,main]take Thread-7979
Thread[Thread-189,5,main]take Thread-8585
Thread[Thread-183,5,main]take Thread-6161
Thread[Thread-192,5,main]take Thread-5959
Thread[Thread-187,5,main]take Thread-9898
Thread[Thread-185,5,main]take Thread-9696
Thread[Thread-179,5,main]take Thread-6363
Thread[Thread-194,5,main]take Thread-5757
Thread[Thread-181,5,main]take Thread-9494
Thread[Thread-190,5,main]take Thread-6565
Thread[Thread-177,5,main]take Thread-9191
Thread[Thread-196,5,main]take Thread-5555
Thread[Thread-175,5,main]take Thread-9292
Thread[Thread-188,5,main]take Thread-6767
Thread[Thread-191,5,main]take Thread-5353
Thread[Thread-198,5,main]take Thread-9393
Thread[Thread-186,5,main]take Thread-8787
Thread[Thread-193,5,main]take Thread-9595
Thread[Thread-195,5,main]take Thread-9999
Thread[Thread-197,5,main]take Thread-9797
Thread[Thread-199,5,main]take Thread-5151


多线程之线程通信条件Condition