代码如下:
package threadSleep;
import java.util.ArrayList;
class UsefullObj{
}
class Storage{
ArrayList<UsefullObj> arrayUObj = new ArrayList<UsefullObj>(5);
ArrayList<UsefullObj> getArrayUObj(){
return arrayUObj;
}
public void push(UsefullObj usefullObj){
synchronized (arrayUObj) {
while(arrayUObj.size() >= 5){
try{
arrayUObj.wait();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
arrayUObj.add(usefullObj);
//System.out.println(Thread.currentThread().getId());
System.out.println(Thread.currentThread().getId()+" thread push storage size = "+arrayUObj.size());
arrayUObj.notifyAll();
}
}
public UsefullObj pop() throws InterruptedException{
synchronized(arrayUObj){
while(arrayUObj.size() <= 0){
arrayUObj.wait();
}
UsefullObj uObj = arrayUObj.remove(0);
//System.out.println(Thread.currentThread().getId());
System.out.println(Thread.currentThread().getId()+" thread pop storage size = "+arrayUObj.size());
arrayUObj.notifyAll();
return uObj;
}
}
}
class Produce implements Runnable{
private Storage storage;
public Produce(Storage storage) {
// TODO Auto-generated constructor stub
this.storage = storage;
}
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0;i < 6;++i){
UsefullObj usefullObj = new UsefullObj();
this.storage.push(usefullObj);
}
}
}
class Consumers implements Runnable{
private Storage storage;
public Consumers(Storage storage) {
// TODO Auto-generated constructor stub
this.storage = storage;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
for (int i = 0;i < 6;++i){
this.storage.pop();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class MyProduceComsume {
public static void main(String[] args) {
Storage gStorage = new Storage();
//System.out.println("arrylist size"+gStorage.getArrayUObj().size());
Produce objProduce1 = new Produce(gStorage);
Produce objProduce2 = new Produce(gStorage);
Consumers objConsume1 = new Consumers(gStorage);
Consumers objConsume2 = new Consumers(gStorage);
Thread tProduce1 = new Thread(objProduce1);
Thread tConsumer1 = new Thread(objConsume1);
Thread tProduce2 = new Thread(objProduce2);
Thread tConsumer2 = new Thread(objConsume2);
tProduce1.start();
tConsumer1.start();
tConsumer2.start();
tProduce2.start();
}
}
结果:
11 thread push storage size = 1
11 thread push storage size = 2
11 thread push storage size = 3
11 thread push storage size = 4
11 thread push storage size = 5
14 thread pop storage size = 4
14 thread pop storage size = 3
14 thread pop storage size = 2
14 thread pop storage size = 1
14 thread pop storage size = 0
13 thread push storage size = 1
13 thread push storage size = 2
13 thread push storage size = 3
13 thread push storage size = 4
13 thread push storage size = 5
12 thread pop storage size = 4
12 thread pop storage size = 3
12 thread pop storage size = 2
12 thread pop storage size = 1
12 thread pop storage size = 0
11 thread push storage size = 1
14 thread pop storage size = 0
13 thread push storage size = 1
12 thread pop storage size = 0