使用jdk中的lock和condition机制实现生产者和消费者
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ProducerConsumerTraditional {
public static void main(String[] args) {
final Resources r = new Resources();
new Thread(() -> {
for(int i = 1; i <= 5; i ++){
r.increment();
}
},"producer").start();
new Thread(() -> {
for(int i = 1; i <= 5; i ++){
r.decrement();
}
},"consumer").start();
}
}
class Resources{
//规定,0 我们就开始生产 1 我们就进行消费
private int number = 0;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
//对number进行加一操作 生产者
public void increment(){
lock.lock();
try {
while(number != 0){
condition.await();
}
number ++;
System.out.println(Thread.currentThread().getName() + "\t" + number);
//通知消费者消费
condition.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
//对number进行减一 消费者
public void decrement(){
lock.lock();
try {
while(number == 0){
condition.await();
}
number --;
System.out.println(Thread.currentThread().getName() + "\t" + number);
//通知生产者生产
condition.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
使用BlockingQueue实现升级版的生产者和消费者
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class ProdAndConsumerBlockingQueue {
public static void main(String[] args) throws Exception {
BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
MyResources resources = new MyResources(blockingQueue);
new Thread(() -> {
System.out.println("生产线程启动");
try {
resources.producer();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"producer").start();
new Thread(() -> {
System.out.println("消费线程启动");
try {
resources.consumer();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"consumer").start();
TimeUnit.SECONDS.sleep(5);
resources.stop();
}
}
class MyResources{
private volatile boolean flag = true;
AtomicInteger number = new AtomicInteger(0);
BlockingQueue<String> blockingQueue = null;
public MyResources(BlockingQueue<String> blockingQueue){
this.blockingQueue = blockingQueue;
System.out.println(blockingQueue.getClass().getName());
}
public void producer() throws InterruptedException {
String data = null;
boolean b ;
while(flag){
data = number.incrementAndGet() + "";
//放入blockQueue中
b = blockingQueue.offer(data,2L,TimeUnit.SECONDS);
if(b) {
System.out.println(Thread.currentThread().getName() + "\t" + "放入队列" + data + "成功!");
}else{
System.out.println(Thread.currentThread().getName() + "\t" + "放入队列" + data + "失败!");
}
TimeUnit.SECONDS.sleep(1);
}
System.out.println(Thread.currentThread().getName() + "\t" + "生产终止");
}
public void consumer() throws InterruptedException {
String result = null;
while(flag){
result = blockingQueue.poll(2L,TimeUnit.SECONDS);
if(null == result || "".equalsIgnoreCase(result)){
flag = false;
System.out.println(Thread.currentThread().getName() + "\t" + "超过2秒,消费退出");
return;
}
System.out.println(Thread.currentThread().getName() + "\t" + "取出队列" + result + "成功!");
}
}
public void stop(){
this.flag = false;
}
}