java 11-10的test

class TenAnianm4{
public static void main(String[] agrs) {
Pool pool=new Pool();
Bear b = new Bear("B1",pool);
//Bear b1 = new Bear("B2",pool);
Cat c = new Cat("C",pool);
b.start();
//b1.start();
c.start();

}

}

class Pool {
private int Max=20;
private java.util.List<Integer> list =new java.util.LinkedList<Integer>();
public void addMI(int i){
synchronized (this ){
while (list.size()>=Max){
try{
this.wait(2000);
}
catch(Exception e){

}

}

list.add(new Integer(i));
this.notifyAll();
}
}

public int Remove() {
synchronized (this ){
while ((list.size())<Max){
try{
this.wait(2000);
}
catch(Exception e){

}
}
this.notifyAll();
list.clear();
return list.size();
}
}
}

class Bear extends Thread{
static int i=1;
private String name;
private Pool pool;
public Bear(String name,Pool pool){
this.name=name;
this.pool=pool;
}

public void run(){
while(true){
pool.addMI(i);
System.out.println("生产了=====》》" + i +"g");
i++;
}
}

}


class Cat extends Thread{
private String name;
private Pool pool;
public Cat(String name,Pool pool){
this.name=name;
this.pool=pool;
}

public void run(){
while(true){
pool.Remove();
System.out.println("吃掉了==》》");
}
}

}

原文地址:https://www.cnblogs.com/simly/p/10863327.html