一、商品类
import javax.swing.*;
import java.util.LinkedList;
import java.util.Queue;
public class Goods {
private String id;
private String name;
private double price;
public Goods(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "Goods{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
二、生产者
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.UUID;
public class producer implements Runnable {
private final Queue <Goods> queue;
public producer(Queue <Goods> queue) {
this.queue = queue;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep ( 1000 );
} catch (InterruptedException e) {
e.printStackTrace ( );
}
synchronized (this.queue) {
if (queue.size ( ) > 10) {
System.out.println ( "容器已满,请加快消费!" );
try {
this.queue.wait ( );
} catch (InterruptedException e) {
e.printStackTrace ( );
}
} else {
String id =String.valueOf ( UUID.randomUUID ( ) );
Goods goods = new Goods ( id, "包子", new Random ( ).nextDouble ( ) );
this.queue.add ( goods );
}
}
}
}
}
三、消费者
import java.util.Queue;
public class Consumer implements Runnable {
private final Queue <Goods> queue;
public Consumer(Queue <Goods> queue) {
this.queue = queue;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep ( 1000 );
} catch (InterruptedException e) {
e.printStackTrace ( );
}
synchronized (this.queue) {
if (this.queue.isEmpty ( )) {
System.out.println ( Thread.currentThread ( ).getName ( ) + "容器已空,停止消息,加速生产" );
this.queue.notify ( );
} else {
Goods goods = this.queue.poll ( );
System.out.println ( Thread.currentThread ( ).getName ( ) + "消费商品" + goods );
}
}
}
}
}
四、运用命令行输入进行测试类测试
import java.util.LinkedList;
import java.util.Queue;
public final class ProducerConsumerLauncher {
private ProducerConsumerLauncher(){}
public static void run(int producers,int consumers){
System.out.println("生产者:" + producers + " 消费者:" + consumers);
Queue<Goods> queue=new LinkedList<> ( );
Runnable produce=new producer ( queue );
Runnable consumer=new Consumer ( queue );
for(int i=0;i<producers;i++){
new Thread ( produce,"生产者-"+i ).start ();
}
for(int i=0;i<consumers;i++){
new Thread ( consumer,"消费者-"+i ).start ();
}
}
}
五、测试
import java.util.LinkedList;
import java.util.Queue;
public class TestProducerConsumer {
public static void main(String[] args) {
//方法一:不灵活
/* Queue<Goods> queue=new LinkedList <> ( );
Runnable produce=new producer ( queue );
Runnable consumer=new Consumer ( queue );
for(int i=0;i<5;i++){
new Thread ( produce,"生产者-"+i ).start ();
}
for(int i=0;i<2;i++){
new Thread ( consumer,"消费者-"+i ).start ();
}*/
//方法二:输命令行参数测试
int defaultProducers=5;
int defaultConsumers=5;
int producers=defaultProducers;
int consumers=defaultConsumers;
if(args.length==1){
producers=Integer.parseInt ( args[0] );
}
if(args.length==2){
producers=Integer.parseInt ( args[0] );
consumers=Integer.parseInt ( args[1] );
}
if(producers<0){
producers=defaultProducers;
}
if(consumers<0){
consumers=defaultConsumers;
}
ProducerConsumerLauncher.run ( producers,consumers );
}
}