生產者消費者模式

//尚學堂視頻裏,不是完整的 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

相關文章
相關標籤/搜索