使用ReentrantLock实现生产者消费者模式
来源:互联网 发布:qq象棋作弊软件 编辑:程序博客网 时间:2024/06/04 20:14
使用ReentrantLock实现生产者消费者模式
一. 生产者/消费者模式:一对一交替打印
新建MyService.java :
package service;
importjava.util.concurrent.locks.Condition;
importjava.util.concurrent.locks.ReentrantLock;
public class MyService {
privateReentrantLock lock = new ReentrantLock();
privateCondition condition = lock.newCondition();
privateboolean hasValue = false;
publicvoid set() {
try{
lock.lock();
while(hasValue == true) {
condition.await();
}
System.out.println("打印★");
hasValue= true;
condition.signal();
}catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
publicvoid get() {
try{
lock.lock();
while(hasValue == false) {
condition.await();
}
System.out.println("打印☆");
hasValue= false;
condition.signal();
}catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
新建MyThreadA.java :
package extthread;
import service.MyService;
public class MyThreadA extends Thread {
privateMyService myService;
publicMyThreadA(MyService myService) {
super();
this.myService= myService;
}
@Override
publicvoid run() {
for(int i = 0; i < Integer.MAX_VALUE; i++) {
myService.set();
}
}
}
新建MyThreadB.java :
package extthread;
import service.MyService;
public class MyThreadB extends Thread {
privateMyService myService;
publicMyThreadB(MyService myService) {
super();
this.myService= myService;
}
@Override
publicvoid run() {
for(int i = 0; i < Integer.MAX_VALUE; i++) {
myService.get();
}
}
}
新建Run.java :
package test;
import service.MyService;
import extthread.MyThreadA;
import extthread.MyThreadB;
public class Run {
publicstatic void main(String[] args) throws InterruptedException {
MyServicemyService = new MyService();
MyThreadAa = new MyThreadA(myService);
a.start();
MyThreadBb = new MyThreadB(myService);
b.start();
}
}
运行结果如下:
二. 生产者/消费者模式:多对多交替打印
新建类MyService.jave:
package service;
importjava.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class MyService {
privateReentrantLock lock = new ReentrantLock();
privateCondition condition = lock.newCondition();
privateboolean hasValue = false;
publicvoid set() {
try{
lock.lock();
while(hasValue == true) {
System.out.println("有可能★★连续");
condition.await();
}
System.out.println("打印★");
hasValue= true;
// 注意:如果signall() 会出现假死情况
condition.signalAll();
}catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
publicvoid get() {
try{
lock.lock();
while(hasValue == false) {
System.out.println("有可能☆☆连续");
condition.await();
}
System.out.println("打印☆");
hasValue= false;
condition.signalAll();
}catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
2个线程类和上面一样。
新建Run.java :
package test;
import service.MyService;
import extthread.MyThreadA;
import extthread.MyThreadB;
public class Run {
publicstatic void main(String[] args) throws InterruptedException {
MyServiceservice = new MyService();
MyThreadA[]threadA = new MyThreadA[10];
MyThreadB[]threadB = new MyThreadB[10];
for(int i = 0; i < 10; i++) {
threadA[i]= new MyThreadA(service);
threadB[i]= new MyThreadB(service);
threadA[i].start();
threadB[i].start();
}
}
}
运行结果如下:
- 使用ReentrantLock实现生产者消费者模式
- ReentrantLock方式实现生产者-消费者模式
- java多线程--ReentrantLock实现生产者与消费者模式
- 使用ReentrantLock和Condition实现生产者消费者模型
- 生产者消费者模式,并发采用ReentrantLock
- synchronized和ReentrantLock实现消费者生产者问题
- java使用LinkedBlockingQueue实现 生产者 消费者模式
- 使用BlockingQueue实现生产者消费者模式
- Java使用BlockingQueue实现生产者消费者模式
- 使用BlockingQueue实现生产者消费者模式
- 使用wait/notify实现生产者消费者模式
- 使用条件变量实现生产者消费者模式
- 使用BlockingQueue实现生产者和消费者模式
- 使用BlockingQueue实现生产者消费者模式
- Python使用生成器实现生产者消费者模式
- 使用BlockingQueue实现生产者,消费者模式
- 生产者消费者模式实现
- 生产者消费者模式实现
- OMRON E6B2-CWZ6C
- RGB
- 网站打开速度慢的原因,排查方法及优化方法(大全)
- java eclipse 的安装流程
- 解決thinkPHP出現no input file specified.問題
- 使用ReentrantLock实现生产者消费者模式
- File类 和 io流
- 一个java程序员的真实经历
- Linux中chown和chmod的区别和用法
- Red packet 【模拟】
- Java面试题全集(上)
- lintcode--删除排序数组中的重复数字 II
- [004]一步一步学懂spring
- 数据库系统概念 读书笔记(五)