//尚学堂视频里,不是完整的 public class Movie { /** * 共同的资源 */ private String pic;
//flay为true生产,false消费
private boolean flag=true;
public synchronized void play(String pic) throws InterruptedException { if(!flag)
this.wait();
Thread.sleep(500);
this.pic=pic;
this.notify();
this.flag=false;
}
public synchronized void watch() throws InterruptedException {
if (flag)
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread.sleep(200);
System.out.println(pic);
this.notifyAll();
this.flag=true;
} }