- 1.使用wait()和notify()模拟
Consumer类:
import java.util.ArrayList;
/**
* @author hetiantian
* 模拟消费者线程
*/
public class Consumer implements Runnable {
private static ArrayList<Food> foods = new ArrayList<>(5);
public Consumer(ArrayList<Food> foods) {
this.foods = foods;
}
public static void eatFood() throws InterruptedException {
while (true) {
int last = foods.size() - 1;
Food f;
synchronized (foods) {
if (last < 0) {
foods.notify(); //如果吃光了则释放锁让生产者继续执行
} else {
f = foods.get(last);
System.out.println("消费了:" + f);
foods.remove(last);
}
}
}
}
@Override
public void run() {
try {
eatFood();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Product类:
import java.util.ArrayList;
/**
* @author hetiantian
* 模拟生产者生产食物
*/
public class Producer implements Runnable {
//一次最多只能生产五个,如果到达五个了就需等待消费者把它吃了才能继续生产
private static ArrayList<Food> foods = new ArrayList<>(5);
static int id; //事物标记
public Producer(ArrayList<Food> foods) {
this.foods = foods;
}
//模拟生产的过程
public static void productFood() throws InterruptedException {
while (true) {
Food f = new Food(id++);
synchronized (foods) {
if (foods.size() >= 5) {
f.wait(); //如果大于等于6将被挂起,只有被消费了以后才会继续执行下一步的操作
} else {
foods.add(f);
System.out.println("生产了:" + f);
}
}
}
}
@Override
public void run() {
try {
productFood();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
测试类:
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* @author hetiantian
* 消费者生产者测试类
*/
public class ProductAndConsumerTest {
private static ArrayList<Food> foods = new ArrayList<>(5);
public static void main(String[] args) throws InterruptedException {
Producer p = new Producer(foods);
Consumer c = new Consumer(foods);
ExecutorService es = Executors.newCachedThreadPool();
es.execute(p);
es.execute(c);
TimeUnit.SECONDS.sleep(7);
//关闭任务
es.shutdownNow();
}
}
- 2.使用BlockingQueue模拟生产者消费者,这里我选择的是ArrayBlockingQueue
厨师类:
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
* @author hetiantain
*/
public class Chief implements Runnable {
private static BlockingQueue<Food> blockingQueue = new ArrayBlockingQueue<>(5);
static int id = 0;
public Chief(BlockingQueue<Food> blockingQueue) {
this.blockingQueue = blockingQueue;
}
public static void createFood() throws InterruptedException {
Food f = new Food(id++);
blockingQueue.put(f);
System.out.println("生产了: " + f);
}
@Override
public void run() {
while (true) {
try {
createFood();
} catch (InterruptedException e) {
break;
}
}
}
}
食客类:
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
* @author hetiantian
* 模拟食客
*/
public class Diners implements Runnable {
private static BlockingQueue<Food> blockingQueue = new ArrayBlockingQueue<>(5);
public Diners(BlockingQueue<Food> blockingQueue) {
this.blockingQueue = blockingQueue;
}
public static void eatFood() throws InterruptedException {
Food f = blockingQueue.take();
System.out.println("消费了 " + f);
}
@Override
public void run() {
while (true) {
try {
eatFood();
} catch (InterruptedException e) {
break;
}
}
}
}
使用阻塞队列不需要人为的去维护,使用起来非常方便
3- 使用Condition的wait()和signal()模拟生产者消费者问题
生产者:
import java.util.ArrayList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
/**
* @author hetiantian
* 模拟生产者往篮子(arrayList)里添加事物,篮子的容量大小为5
*/
public class Producer implements Runnable {
private ArrayList<Food> arrayList;
private static int id; //标记事物id
Lock lock;
Condition notFull;
Condition notEmpty;
public Producer(ArrayList<Food> arrayList, Lock lock, Condition notFull, Condition notEmpty) {
this.arrayList = arrayList;
this.lock = lock;
this.notFull = notFull;
this.notEmpty = notEmpty;
}
@Override
public void run() {
//保证只有一个线程能够获得同步状态
lock.lock();
try {
while (true) {
Food f = new Food(++id);
System.out.println("生产了" + f);
arrayList.add(f);
notEmpty.signal();
if (arrayList.size() == 5) { //等于5的时候不能继续增加
System.out.println("篮子满了,不能继续生产了,等待消费");
notFull.await(); //当前线程进入等待队列中
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
消费者:
import java.util.ArrayList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
/**
* @author hetiantian
* 消费类,篮子为空则需要等待直至有食物可以消费
*/
public class Consumer implements Runnable {
private ArrayList<Food> arrayList;
Lock lock;
Condition notFull;
Condition notEmpty;
public Consumer(ArrayList<Food> arrayList, Lock lock, Condition notFull, Condition notEmpty) {
this.arrayList = arrayList;
this.lock = lock;
this.notFull = notFull;
this.notEmpty = notEmpty;
}
@Override
public void run() {
lock.lock();
try {
while (true) {
int last = arrayList.size() - 1;
Food food = arrayList.get(last);
System.out.println("移除了" + food);
arrayList.remove(food);
notFull.signal();
if (arrayList.size() == 0) { //消费以后如果篮子刚好为空,则进行等待
System.out.println("篮子为空,进入等待状态,等待生产");
notEmpty.await();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
}
}
测试类:
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author hetiantian
* 生产者消费者测试类
*/
public class ConsumerAndProducerTest {
public static void main(String[] args) throws InterruptedException {
ExecutorService es = Executors.newCachedThreadPool();
ArrayList<Food> arrayList = new ArrayList<>(5);
Lock lock = new ReentrantLock();
Condition notFull = lock.newCondition();
Condition notEmpty = lock.newCondition();
Producer p = new Producer(arrayList, lock, notFull,notEmpty);
Consumer c = new Consumer(arrayList, lock, notFull,notEmpty);
es.execute(p);
Thread.sleep(1000); //让主线程睡眠1秒
es.execute(c);
Thread.sleep(1000); //让主线程睡眠1秒
es.shutdownNow(); //关闭线程执行
}
}
使用Condition和使用wait()和notify()相似,都需要先获得锁,然后人为的实现等待和同步
小结:使用BlockingQueue(阻塞队列)来模拟消费者生产者问题更为方便,未使用阻塞队列时,必须额外地实现同步策略以及线程间唤醒策略。而使用阻塞队列时,当队列为空时,消费者线程将被阻塞,直到队列不为空时将被自动唤醒,而当队列满时,生产者线程将被阻塞,直到队列不满时将被自动唤醒。
注:为什么在捕捉到异常的代码块中使用break。因为调用es.shutdownNow()底层仍然是通过调用interupt()方法结束任务,如果不调用break,仍然会继续执行,interupt()只是告诉线程需要中断,并在合适的时间点中断(目前自己的理解,以后了解更多更深补充和修改)