文章插图
类中的wait()相当于类中的await()方法
类的wait(long )相当于类中的await(long time,unit)方法
类的 ()相当于的()
的()相当于的()
24.3 使用多个实现通知部分线程
文章插图
文章插图
文章插图
文章插图
文章插图
24.4 生产者--消费者模式实现一对一交替打印:
package com.xkf.reentrantLock.demo.procon;/*** 生产者-消费者模式:* 一对一交替打印* @author katy.xie**/public class ConditionProducerConsumer {public static void main(String[] args) {ProConService service = new ProConService();ThreadB b = new ThreadB(service);b.start();ThreadC c = new ThreadC(service);c.start();}}
package com.xkf.reentrantLock.demo.procon;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class ProConService {private ReentrantLock lock = new ReentrantLock();private Condition condition = lock.newCondition();private Boolean hasvalue = http://www.kingceram.com/post/false;public void set(){try {lock.lock();while(hasvalue == true){condition.await();}System.out.println("----");hasvalue = http://www.kingceram.com/post/true;condition.signal();} catch (Exception e) {e.printStackTrace();}finally{lock.unlock();}}public void get(){try {lock.lock();while(hasvalue == false){condition.await();}System.out.println("++++");hasvalue = http://www.kingceram.com/post/false;condition.signal();} catch (Exception e) {e.printStackTrace();}finally{lock.unlock();}}}
package com.xkf.reentrantLock.demo.procon;public class ThreadB extends Thread{private ProConService service ;public ThreadB(ProConService service) {this.service = service;}@Overridepublic void run() {for (int i = 0; i < 10; i++) {service.set();}}}
package com.xkf.reentrantLock.demo.procon;public class ThreadC extends Thread{private ProConService service ;public ThreadC(ProConService service) {this.service = service;}@Overridepublic void run() {for (int i = 0; i < 10; i++) {service.get();}}}
24.5生产者--消费者模式实现多对多交替打印:
package com.xkf.reentrantLock.demo.procon;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class AlternateProConService {private ReentrantLock lock = new ReentrantLock();private Condition condition = lock.newCondition();private Boolean hasValue = http://www.kingceram.com/post/false;public void produce(){try {lock.lock();while(hasValue == true){System.out.println("有可能++连续");condition.await();}System.out.println("打印++");hasValue = http://www.kingceram.com/post/true;condition.signalAll();} catch (Exception e) {e.printStackTrace();}finally{lock.unlock();}}public void consume(){try {lock.lock();while(hasValue == false){System.out.println("有可能--连续");condition.await();}System.out.println("打印--");hasValue = http://www.kingceram.com/post/false;condition.signalAll();} catch (Exception e) {e.printStackTrace();}finally{lock.unlock();}}}
package com.xkf.reentrantLock.demo.procon;/*** 生产者---消费者模式:交替打印* @author katy.xie**/public class AlternateProCon {public static void main(String[] args) {AlternateProConService service = new AlternateProConService();ThreadD[] d = new ThreadD[10];ThreadE[] e = new ThreadE[10];for (int i = 0; i
- 中秋国庆期间湖北省西部多雨日 东部阴雨相间
- 未来几天陕西省维持阴雨天气 西安以多云到阴天为主
- 邮箱邮件服务器迁移服务器要多久生效,将设置从电子邮件路由器迁移到服务器端同步
- 基于微信小程序的美妆电子商务系统(java+spring+springMvc+s
- 假期佛山市天气总体平稳 以多云天气为主
- 【Java入门】-- Java基础详解之 [变量]
- java基础语法总复习思维导图 + 重难点+面试题
- 生成一个适合你的列表
- 【已解决】Java 中导入excel时使用 trim 无法去除空格的解决方法
- 编码: 一个隐藏了30多年的bug,Windows含蓄说过某通不行?