//尚学堂视频里,不是完整的 public class Movie { /** * 共同的资源 */ private String pic;this
//flay为true生产,false消费code
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;
}it
public synchronized void watch() throws InterruptedException {io
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;
} }class