package cn.Douzi.ProductConsume;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class ProCon {
static int LEN = 3;
static public int PROTIME = 1500;
static public int CONTIME = 1000;
static public int totalData = 1000;
static public Queue<Data> buffer = new LinkedList<>();
static public Buffer empty = new Buffer(0, 10);
static public Mutex mutex = new Mutex(1);
public ProCon() {
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("|-----------------------------------------------------------|");
System.out.println("| |");
System.out.println("| Welcome to My System ! |");
System.out.println("| ↖ By Douzi 2017 ↗ |");
System.out.println("| 生产消费开始进行 |");
System.out.println("| |");
System.out.println("|-----------------------------------------------------------|
");
Scanner scaner = new Scanner(System.in);
System.out.println("设置缓冲区大小: ");
LEN = scaner.nextInt();
System.out.println("缓冲区大小: " + LEN + "
");
System.out.println("设置生产休眠时间: ");
ProCon.PROTIME = scaner.nextInt();
System.out.println("设置消费休眠时间: ");
ProCon.CONTIME = scaner.nextInt();
ProductWindows pro = new ProductWindows();
ConsumeWindows con = new ConsumeWindows();
Thread t1 = new Thread(pro);
Thread t2 = new Thread(con);
t1.start(); t2.start();
}
}
class ProductWindows implements Runnable {
public ProductWindows() {
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(ProCon.PROTIME);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ProCon.totalData++;
System.out.println("生产数据:" + ProCon.totalData);
if (ProCon.buffer.size() < ProCon.LEN) {
synchronized (ProCon.mutex) {
ProCon.buffer.add(new Data(ProCon.totalData));
System.out.println("将数据: " + ProCon.totalData + " 放入缓冲区");
System.out.println("此时缓冲区大小: " + ProCon.buffer.size() + "
");
}
} else {
System.out.println("缓冲区满,等待数据取出缓冲区.....................
");
ConsumeWindows con = new ConsumeWindows();
Thread t2 = new Thread(con);
t2.start();
break;
}
}
}
}
class ConsumeWindows implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
// System.out.println("Test");
while (true) {
Data d = new Data(0);
try {
Thread.sleep(ProCon.CONTIME);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (ProCon.buffer.size() > 0) {
synchronized (ProCon.mutex) {
d = ProCon.buffer.peek(); ProCon.buffer.poll();
System.out.println("从缓冲区取出数据: " + d.getData());
}
} else {
System.out.println("缓冲区空,等待数据放入缓冲区..................
");
ProductWindows pro = new ProductWindows();
Thread t1 = new Thread(pro);
t1.start();
break;
}
System.out.println("消费数据: " + d.getData() + "
");
}
}
}
class Data {
private int data;
public Data(int data) {
super();
this.data = data;
}
public int getData() {
return data;
}
}
class Mutex {
private int mutex;
public Mutex(int mutex) {
super();
this.mutex = mutex;
}
}
class Buffer {
private int full;
private int empty;
public Buffer(int full, int empty) {
super();
this.full = full;
this.empty = empty;
}
}