Java多线程编程核心技术( 六 )


Java多线程编程核心技术

文章插图
类中的wait()相当于类中的await()方法
类的wait(long )相当于类中的await(long time,unit)方法
类的 ()相当于的()
的()相当于的()
24.3 使用多个实现通知部分线程
Java多线程编程核心技术

文章插图

Java多线程编程核心技术

文章插图

Java多线程编程核心技术

文章插图

Java多线程编程核心技术

文章插图

Java多线程编程核心技术

文章插图
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