多线程之线程通信条件Condition

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

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

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

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
时间: 2025-01-02 03:04:09

多线程之线程通信条件Condition的相关文章

多线程之线程通信条件Condition二

接上一篇,实现Condition三个条件,有这样一个应用: 1. 有三个进程,第一个进程运行1次,第二个进程运行2次,第三个进程运行3次: 2. 先运行第二个进程,然后第一个,然后第三个: 3.  依次运行5次循环. 分析: 此时若用Object的wait和notify是实现不了的,我们能够用Lock锁的Condition实现,我们须要定义三个信号条件,分别控制这三个进程. 实现例如以下: package andy.thread.test; import java.util.concurrent

线程基础知识系列(四)线程的同步2 线程通信和Condition变量

本文是系列的第四篇. 线程基础知识系列(三)线程的同步  :同步控制,锁及synchronized 线程基础知识系列(二)线程的管理 :线程的状态,控制,休眠,Interrupt,yield等 线程基础知识系列(一)线程的创建和启动  :线程的创建和启动,join(),daemon线程,Callable任务. 第三篇文章,重点阐述了如何使用锁和同步块对线程间共享可变变量保护,保证只有一个线程可以进入临界区.其实,没有过多的涉及另一个重要的同步概念:线程协作.第三篇中涉及的线程间并没有有效的协调.

JavaSE:多线程补充--线程通信

线程通信我认为是多线程中最难掌握的部分了,这里通过两个例子来说明一下. 第一个: 使用两个线程打印 1-100. 线程1, 线程2 交替打印 public class Print implements Runnable{ int i = 1; public void run(){ while(true){ synchronized(this){ if(i<100){ notify(); System.out.println(Thread.currentThread().getName() + &qu

Java多线程之线程通信

线程通信的例子:使用两个线程打印 1-100,线程1.线程2交替打印.涉及到的三个方法:wait():一旦执行此方法,当前线程就进入阻塞状态,并释放同步监视器.notify():一旦执行此方法,就会唤醒被wait的一个线程.如果有多个线程被wait,就唤醒优先级高的那个.notifyAll():一旦执行此方法,就会唤醒所有被wait的线程. 说明:1.wait(),notify(),notifyAll()三个方法必须使用在同步代码块或同步方法中.2.wait(),notify(),notifyA

Java之多线程开发时多条件Condition接口的使用

转:http://blog.csdn.net/a352193394/article/details/39454157 我们在多线程开发中,可能会出现这种情况.就是一个线程需要另外一个线程满足某某条件才能继续运行,或者需 要其他线程满足好几个条件才能运行,对于这样的多条件的多线程并发,我们如何控制好各个线程之间的关系,使他们 能很好的处理冲突不至于相互出现问题呢,下面我们来介绍一下Java提供的Condition这个接口,这个接口很好的实现了 这种需求. 对于这个问题最经典的例子就是生产者消费者模

Java多线程开发之~~~多条件Condition接口的使用

我们在多线程开发中,可能会出现这种情况.就是一个线程需要另外一个线程满足某某条件才能继续运行,或者需 要其他线程满足好几个条件才能运行,对于这样的多条件的多线程并发,我们如何控制好各个线程之间的关系,使他们 能很好的处理冲突不至于相互出现问题呢,下面我们来介绍一下Java提供的Condition这个接口,这个接口很好的实现了 这种需求. 对于这个问题最经典的例子就是生产者消费者模型,生产者当缓冲区满的时候不生产商品知道缓冲区有空余,消费 者当缓冲区为0 的时候不拿商品,直到生产者向缓冲区放入商品

Android 多线程及线程通信

AsyncTask AsyncTask,它使创建需要与用户界面交互的长时间运行的任务变得更简单.相对来说AsyncTask更轻量级一些,适用于简单的异步处理,不需要借助线程和Handler即可实现. AsyncTask是抽象类.AsyncTask定义了三种泛型类型 Params,Progress和Result. Params 启动任务执行的输入参数,比如HTTP请求的URL. Progress 后台任务执行的百分比. Result 后台执行任务最终返回的结果,比如String. AsyncTas

AsyncTask、多线程及线程通信

AsyncTask,它使创建需要与用户界面交互的长时间运行的任务变得更简单.相对来说AsyncTask更轻量级一些,适用于简单的异步处理,不需要借助线程和Handler即可实现. AsyncTask是抽象类.AsyncTask定义了三种泛型类型 Params,Progress和Result. Params 启动任务执行的输入参数,比如HTTP请求的URL. Progress 后台任务执行的百分比. Result 后台执行任务最终返回的结果,比如String. AsyncTask的执行分为四个步骤

java 多线程 day04 线程通信

package com.czbk.thread; /** * Created by chengtao on 17/12/3. * 需求: 子线程先运行10次,然后主线程运行 100次,依次运行50次 * wait(): 等待 如果线程执行了wait方法,那么该线程会进入等待的状态,等待状态下的线程必须要被其他线程调用notify方法才能唤醒. notify(): 唤醒 唤醒线程池等待线程其中的一个. notifyAll() : 唤醒线程池所有等待 线程. wait与notify方法要注意的事项: