仓库
public class MyQueue {
private static List<String> queueList = Collections.synchronizedList(new ArrayList<String>());
/**
* 循环次数
*/
private volatile static int cycleCount = 30;
/**
* 生产者操作次数
*/
public volatile static int proOperCount = 0;
/**
* 消费者操作次数
*/
public volatile static int conOperCount = 0;
/**
* 仓库最大库存
*/
public volatile static int maxStorageCount = 1;
/**
* 获取仓库对象
* @return
*/
public static List<String> getQueueList() {
return queueList;
}
/**
* 基于仓库对象当前线程等待
*/
public static void queueWait(){
try {
queueList.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}catch (IllegalMonitorStateException m){
m.printStackTrace();
}
}
/**
* 基于仓库对象当前线程指定时间等待
* @param time
*/
public static void queueWaitTime(int time){
try {
queueList.wait(time);
} catch (InterruptedException e) {
e.printStackTrace();
}catch (IllegalMonitorStateException m){
m.printStackTrace();
}
}
/**
* 基于仓库对象唤醒所有线程
*/
public static void queueNotifyAll(){
try{
queueList.notifyAll();
}catch (IllegalMonitorStateException m){
m.printStackTrace();
}
}
public static int getCycleCount() {
return cycleCount;
}
public static void setCycleCount(int cycleCount) {
MyQueue.cycleCount = cycleCount;
}
public static int getMaxStorageCount() {
return maxStorageCount;
}
public static void setMaxStorageCount(int maxStorageCount) {
MyQueue.maxStorageCount = maxStorageCount;
}
public static int getProOperCount() {
return proOperCount;
}
public synchronized static void setProOperCount(int proOperCount) {
MyQueue.proOperCount = proOperCount;
}
public static int getConOperCount() {
return conOperCount;
}
public synchronized static void setConOperCount(int conOperCount) {
MyQueue.conOperCount = conOperCount;
}
}
生产者
public class Producer implements Runnable {
public void put() throws InterruptedException {
if (MyQueue.getQueueList().size() == MyQueue.getMaxStorageCount()) {
System.out.println(Thread.currentThread().getName().toString()+"生产者等待");
MyQueue.queueWaitTime(1);
} else {
int count = MyQueue.getProOperCount() + 1;
MyQueue.setProOperCount(count);
String str = String.valueOf(Math.random());
MyQueue.getQueueList().add(str);
System.out.println(Thread.currentThread().getName().toString()+"生产:" + str + ", 生产数:"+count);
MyQueue.queueWaitTime(1);
}
}
@Override
public void run() {
try {
while (MyQueue.getProOperCount()< MyQueue.getCycleCount()) {
synchronized (MyQueue.getQueueList()) {
put();
MyQueue.queueNotifyAll();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
消费者
public class Consumer implements Runnable {
public void get() throws InterruptedException {
if (MyQueue.getQueueList().size() == 0) {
System.out.println(Thread.currentThread().getName().toString()+"消费者等待");
MyQueue.queueWaitTime(1);
} else {
int count = MyQueue.getConOperCount() + 1;
MyQueue.setConOperCount(count);
System.out.println(Thread.currentThread().getName().toString()+"消费:"+MyQueue.getQueueList().remove(0) + ", 消费数:"+count);
MyQueue.queueWaitTime(1);
}
}
@Override
public void run() {
try {
while (MyQueue.getConOperCount()< MyQueue.getCycleCount()) {
synchronized (MyQueue.getQueueList()) {
get();
MyQueue.queueNotifyAll();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
生产者消费者入口
public class pandcDemo {
public static void main(String[] args) {
MyQueue.setCycleCount(100);
MyQueue.setMaxStorageCount(1);
Thread proOne = new Thread(new Producer());
Thread proTwo = new Thread(new Producer());
Thread proThree = new Thread(new Producer());
Thread conOne = new Thread(new Consumer());
Thread conTwo = new Thread(new Consumer());
proOne.start();
proTwo.start();
proThree.start();
conOne.start();
conTwo.start();
}
}