class Product{
static int count;//计数
static int NUM = 10;//缓冲区资源数
public static void main(String[] args){
Product p = new Product();
System.out.println("缓冲池总共能放"+NUM+"个商品");
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
Producer pd = new Producer(p,1);
pd.start();
Consumer cs = new Consumer(p,1);
cs.start();
Consumer cs1 = new Consumer(p,2);
cs1.start();
Consumer cs2 = new Consumer(p,3);
cs2.start();
Producer pd1 = new Producer(p,2);
pd1.start();
}
}
class Producer extends Thread{
Product p;int s;
Producer(Product p,int s ){
this.p = p;
this.s=s;
}
public void run(){
while(true){
if(produce())
continue;
else
break;
}
}
boolean produce(){
synchronized (p) {//锁,对象监视器
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
Product.count++;
//Product.NUM++;
if(Product.count >= 1){
p.notify();
System.out.println("第"+s+"个"+"生产者生产"+Product.count+"个商品");
}
if(Product.count >= Product.NUM){
return false;
}else{
return true;
}
}
}
}
class Consumer extends Thread{
Product p;int s;
Consumer(Product p,int s ){
this.p = p;
this.s =s;
}
public void run(){
while(true){
consume();
}
}
void consume(){
synchronized (p) {
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
if(Product.count >= 1){
Product.count--;
// Product.NUM--;
System.out.println("第"+s+"个"+"消费者消费"+Product.count+"个商品");
}else{
try {
p.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
static int count;//计数
static int NUM = 10;//缓冲区资源数
public static void main(String[] args){
Product p = new Product();
System.out.println("缓冲池总共能放"+NUM+"个商品");
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
Producer pd = new Producer(p,1);
pd.start();
Consumer cs = new Consumer(p,1);
cs.start();
Consumer cs1 = new Consumer(p,2);
cs1.start();
Consumer cs2 = new Consumer(p,3);
cs2.start();
Producer pd1 = new Producer(p,2);
pd1.start();
}
}
class Producer extends Thread{
Product p;int s;
Producer(Product p,int s ){
this.p = p;
this.s=s;
}
public void run(){
while(true){
if(produce())
continue;
else
break;
}
}
boolean produce(){
synchronized (p) {//锁,对象监视器
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
Product.count++;
//Product.NUM++;
if(Product.count >= 1){
p.notify();
System.out.println("第"+s+"个"+"生产者生产"+Product.count+"个商品");
}
if(Product.count >= Product.NUM){
return false;
}else{
return true;
}
}
}
}
class Consumer extends Thread{
Product p;int s;
Consumer(Product p,int s ){
this.p = p;
this.s =s;
}
public void run(){
while(true){
consume();
}
}
void consume(){
synchronized (p) {
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
if(Product.count >= 1){
Product.count--;
// Product.NUM--;
System.out.println("第"+s+"个"+"消费者消费"+Product.count+"个商品");
}else{
try {
p.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}