线程 wait notifyAll

来源:互联网 发布:淘宝购物车不能结算 编辑:程序博客网 时间:2024/06/10 18:27
public class Resource {


    private static final int size = 10;
    private List<String> container = new ArrayList<String>(size);


    public synchronized void get() throws InterruptedException {
if (container.isEmpty()) {
   System.out.println(Thread.currentThread().getName() + " waited");
   this.wait();
   System.out.println("test can execute ? ");
}else{
   int size2 = container.size();
   container.remove(size2 - 1);
   System.out.println(Thread.currentThread().getName() + "Real get..");   
}


    }


    public synchronized void put() throws InterruptedException {
if (container.size() > 10) {
   this.wait();
}
System.out.println("NotifyAll executed");
container.add("1");
notifyAll();
System.out.println(" Notifall continue ;");
    }
}
0 0
原创粉丝点击