【Java并发编程实战】-----“J.U.C”:Condition

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體:   |    |    |  超大  

在看Condition之前,我们先来看下面这个例子:

工厂类,用来存放、取出商品:

publicclassDepot {PRivateintdepotSize;//仓库大小privateLock lock;//独占锁publicDepot(){

depotSize= 0;

lock=newReentrantLock();

}/*** 商品入库

*@paramvalue*/publicvoidput(intvalue){try{

lock.lock();

depotSize+=value;

System.out.println(Thread.currentThread().getName()+ " put " + value +" ----> the depotSize: " +depotSize);

}finally{

lock.unlock();

}

}/*** 商品出库

*@paramvalue*/publicvoidget(intvalue){try{

lock.lock();

depotSize-=value;

System.out.println(Thread.currentThread().getName()+ " get " + value +" ----> the depotSize: " +depotSize);

}finally{

lock.unlock();

}

}

}

生产者,生产商品,往仓库里面添加商品:

publicclassProducer {privateDepot depot;publicProducer(Depot depot){this.depot =depot;

}publicvoidproduce(finalintvalue){newThread(){publicvoidrun(){

depot.put(value);

}

}.start();

}

}

消费者,消费商品,从仓库里面取出商品:

publicclassCustomer {privateDepot depot;publicCustomer(Depot depot){this.depot =depot;

}publicvoidconsume(finalintvalue){newThread(){publicvoidrun(){

depot.get(value);

}

}.start();

}

}

测试类:

publicclassTest {publicstaticvoidmain(String[] args) {

Depot depot=newDepot();

Producer producer=newProducer(depot);

Customer customer=newCustomer(depot);

producer.produce(10);

customer.consume(5);

producer.produce(20);

producer.produce(5);

customer.consume(35);

}

}

运行结果:

Thread-0 put 10 ----> the depotSize: 10Thread-1 get 5 ----> the depotSize: 5Thread-2 put 20 ----> the depotSize: 25Thread-3 put 5 ----> the depotSize: 30Thread-4 get 35 ----> the depotSize: -5

程序的运行结果是没有错误的,先put10、然后get5、put20、put5、get35。程序运行结果非常正确,但是在现实生活中,这个实例存在两处错误:

第一:仓库的容量是有限的,我们不可能无限制的往仓库里面添加商品。

第二:仓库的容量是不可能为负数的,但是最后的结果为-5,与现实存在冲突。

针对于上面两处错误,怎么解决?这就轮到Condition大显神通了。

Condition通过前面几篇博客我们知道Lock提供了比synchronized更加强大、灵活的锁机制,它从某种程度上来说替代了synchronized方式的使用。Condition从字面上面理解就是条件。对于线程而言它为线程提供了一个含义,以便在某种状态(条件Condition)可能为true的另一个线程通知它之前,一直挂起该线程。

对于Condition,JDK API中是这样解释的:

Condition 将 Object 监视器方法(wait、notify 和 notifyAll)分解成截然不同的对象,以便通过将这些对象与任意 Lock 实现组合使用,为每个对象提供多个等待 set(wait-set)。其中,Lock 替代了 synchronized 方法和语句的使用,Condition 替代了 Object 监视器方法的使用。

条件(也称为条件队列 或条件变量)为线程提供了一个含义,以便在某个状态条件现在可能为 true 的另一个线程通知它之前,一直挂起该线程(即让其“等待”)。因为访问此共享状态信息发生在不同的线程中,所以它必须受保护,因此要将某种形式的锁与该条件相关联。等待提供一个条件的主要属性是:以原子方式 释放相关的锁,并挂起当前线程,就像 Object.wait 做的那样。

Condition 实例实质上被绑定到一个锁上。要为特定 Lock 实例获得 Condition 实例,请使用其newCondition() 方法。下面我们通过Condition来解决上面的问题:这里只改仓库Depot的代码:

publicclassDepot {privateintdepotSize;//仓库大小privateLock lock;//独占锁privateintcapaity;//仓库容量privateCondition fullCondition;privateCondition emptyCondition;publicDepot(){this.depotSize = 0;this.lock =newReentrantLock();this.capaity = 15;this.fullCondition =lock.newCondition();this.emptyCondition =lock.newCondition();

}/*** 商品入库

*@paramvalue*/publicvoidput(intvalue){

lock.lock();try{intleft =value;while(left > 0){//库存已满时,“生产者”等待“消费者”消费while(depotSize >=capaity){

fullCondition.await();

}//获取实际入库数量:预计库存(仓库现有库存 + 生产数量) > 仓库容量 ? 仓库容量 - 仓库现有库存 : 生产数量//depotSize left capaity capaity - depotSize leftintinc = depotSize + left > capaity ? capaity -depotSize : left;

depotSize+=inc;

left-=inc;

System.out.println(Thread.currentThread().getName()+ "----要入库数量: " + value +";;实际入库数量:" + inc + ";;仓库货物数量:" + depotSize + ";;没有入库数量:" +left);//通知消费者可以消费了emptyCondition.signal();

}

}catch(InterruptedException e) {

}finally{

lock.unlock();

}

}/*** 商品出库

*@paramvalue*/publicvoidget(intvalue){

lock.lock();try{intleft =value;while(left > 0){//仓库已空,“消费者”等待“生产者”生产货物while(depotSize <= 0){

emptyCondition.await();

}//实际消费 仓库库存数量 < 要消费的数量 ? 仓库库存数量 : 要消费的数量intdec = depotSize < left ?depotSize : left;

depotSize-=dec;

left-=dec;

System.out.println(Thread.currentThread().getName()+ "----要消费的数量:" + value +";;实际消费的数量: " + dec + ";;仓库现存数量:" + depotSize + ";;有多少件商品没有消费:" +left);//通知生产者可以生产了fullCondition.signal();

}

}catch(InterruptedException e) {

e.printStackTrace();

}finally{

lock.unlock();

}

}

}

test:

publicclassTest {publicstaticvoidmain(String[] args) {

Depot depot=newDepot();

Producer producer=newProducer(depot);

Customer customer=newCustomer(depot);

producer.produce(10);

customer.consume(5);

producer.produce(15);

customer.consume(10);

customer.consume(15);

producer.produce(10);

}

}

运行结果:

Thread-0----要入库数量: 10;;实际入库数量:10;;仓库货物数量:10;;没有入库数量:0Thread-1----要消费的数量:5;;实际消费的数量: 5;;仓库现存数量:5;;有多少件商品没有消费:0Thread-4----要消费的数量:15;;实际消费的数量: 5;;仓库现存数量:0;;有多少件商品没有消费:10Thread-2----要入库数量: 15;;实际入库数量:15;;仓库货物数量:15;;没有入库数量:0Thread-4----要消费的数量:15;;实际消费的数量: 10;;仓库现存数量:5;;有多少件商品没有消费:0Thread-5----要入库数量: 10;;实际入库数量:10;;仓库货物数量:15;;没有入库数量:0Thread-3----要消费的数量:10;;实际消费的数量: 10;;仓库现存数量:5;;有多少件商品没有消费:0

在Condition中,用await()替换wait(),用signal()替换 notify(),用signalAll()替换notifyAll(),对于我们以前使用传统的Object方法,Condition都能够给予实现。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航