单个生产者消费者模型
class Goods{
private String goodsName; //商品名
private int count; //商品数量
public synchronized void set(String goodsName)throws InterruptedException{ //生产方法
if(this.count>0){
System.out.println("还有商品,稍等生产");
wait();
}
this.goodsName=goodsName;
this.count++;
System.out.println(toString());
notify(); //唤醒消费者消费
}
public synchronized void get()throws InterruptedException{ //消费方法
if(this.count==0){
System.out.println("没有商品");
wait();
}
this.count--;
System.out.println(toString());
notify(); //唤醒生产者生产
}
public String toString(){
return "goodsName="+goodsName+",count="+count+"";
}
}
class Producer implements Runnable{
private Goods goods;
public Producer (Goods goods){
super();
this.goods=goods;
}
public void run(){
try{
this.goods.set("汽车");
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
class Consumer implements Runnable{
private Goods goods;
public Consumer(Goods goods){
super();
this.goods=goods;
}
public void run(){
try{
this.goods.get();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public class haha{
public static void main(String[] args) throws InterruptedException{
Goods goods=new Goods();
Thread produceThread=new Thread(new Producer(goods),"生产者线程");
Thread consumerThread=new Thread(new Consumer(goods),"消费者线程");
produceThread.start(); //启动生产者线程
Thread.sleep(1000);
consumerThread.start(); //启动消费者线程
}
}
多个生产者消费者模型
import java.util.ArrayList;
import java.util.List;
class Goods{
private String goodsName; //商品名
private int count; //商品数量
public synchronized void set(String goodsName)throws InterruptedException{ //生产方法
while(this.count>0){
System.out.println("还有商品,稍等生产");
wait();
}
this.goodsName=goodsName;
this.count++;
System.out.println(toString());
notifyAll(); //唤醒全部消费者线程
}
public synchronized void get()throws InterruptedException{ //消费方法
while(this.count==0){
System.out.println("没有商品");
wait();
}
this.count--;
System.out.println(toString());
notifyAll(); //唤醒全部生产者线程
}
public String toString(){
return "goodsName="+goodsName+",count="+count+"";
}
}
class Producer implements Runnable{
private Goods goods;
public Producer (Goods goods){
super();
this.goods=goods;
}
public void run(){
try{
this.goods.set("汽车");
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
class Consumer implements Runnable{
private Goods goods;
public Consumer(Goods goods){
super();
this.goods=goods;
}
public void run(){
try{
this.goods.get();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public class haha{
public static void main(String[] args) throws InterruptedException{
Goods goods=new Goods();
List<Thread> threadList=new ArrayList<>();
//10个生产者线程
for(int i=0;i<10;i++){
Thread produceThread=new Thread(new Producer(goods));
produceThread.setName("生产者线程"+i);
threadList.add(produceThread);
}
//6个消费者线程
for(int i=0;i<10;i++){
Thread consumerThread=new Thread(new Consumer(goods));
consumerThread.setName("生产者线程"+i);
threadList.add(consumerThread);
}
//启动所有线程
for(Thread thread:threadList){
thread.start();
}
}
}
Lock体系的多生产者消费者模型
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Goods{
private int maxCount;
private String name;
private int count;
private Lock lock=new ReentrantLock();
private Condition producerCondition=lock.newCondition(); //生产者等待队列
private Condition consumerCondition=lock.newCondition(); //消费者等待队列
public Goods(int maxCount){
this.maxCount=maxCount;
}
public void setGoods(String name){
lock.lock();
try {
while (count == maxCount) {
System.out.println(Thread.currentThread().getName()+"商品已达上限");
try {
producerCondition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name=name;
count++;
System.out.println(Thread.currentThread().getName()+"生产"+toString());
//唤醒消费者线程
consumerCondition.signalAll();
}
finally{
lock.unlock(); //lock无论怎样都要释放锁
}
}
public void getGoods(){
lock.lock();
try{
while(count==0){
System.out.println(Thread.currentThread().getName()+"商品卖完了");
try{
consumerCondition.await();
}catch(InterruptedException e){
e.printStackTrace();
}
}
count--;
System.out.println(Thread.currentThread().getName()+"消费"+toString());
//唤醒生产者线程
producerCondition.signalAll();
}finally{
lock.unlock();
}
}
public String toString(){
return "name="+name+"count="+count;
}
}
class Producer implements Runnable{
private Goods goods;
public Producer(Goods goods) {
this.goods = goods;
}
public void run(){
while(true){
this.goods.setGoods("生产一台奔驰");
}
}
}
class Consuer implements Runnable{
private Goods goods;
public Consuer(Goods goods) {
this.goods = goods;
}
public void run(){
while(true){
this.goods.getGoods();
}
}
}
public class haha{
public static void main(String[] args) {
Goods goods=new Goods(50);
Producer producer=new Producer(goods);
Consuer consuer=new Consuer(goods);
List<Thread> list=new ArrayList<>();
//5个生产者
for(int i=0;i<5;i++){
Thread thread=new Thread(producer,"生产者"+i);
list.add(thread);
}
//10个消费者
for(int i=0;i<10;i++){
Thread thread=new Thread(consuer,"消费者"+i);
list.add(thread);
}
for(Thread thread1:list){
thread1.start();
}
}