JAvaスレッドコラボレーション、クラシック生産者/消費者モデル(一、synchronized反発)


JAvaマルチスレッドコラボレーション、最も古典的なのは生産者/消費者モデルです.例えば、皿一つ(緩衝区)、もしこの皿にりんごが1つしか入れられないなら、今、皿からりんごを1つ出すなら、まず皿にりんごがあることを保証しなければなりません.もしなければ、入れるまで待たなければなりません.同じように、りんごを皿に入れるなら、皿の中にりんごが入っていないことを保証しなければなりません.もしあれば、持って行ってから置くことができます.下に貼ってください.アップルを入れる/アップルを出すプロセスのスレッドコードを実現します.
/**
 * java    ,   /   
 * @author    
 * 2016 6 20 20:36:56
 */
public class Plate {
	
	private static int plate=0;//      ,    ,       
	//      ,  synchronized   
	public synchronized void putApple() throws InterruptedException {
		while (plate==1) {
			this.wait();//     ,     ,   
		}
		System.out.println("  apple");
		plate=1;
		this.notifyAll();//   ,       
	}
	//      ,  synchronized   
	public synchronized void getApple() throws InterruptedException {
		while (plate==0) {
			this.wait();//      ,    ,         
		}
		System.out.println("    ");
		plate=0;
		this.notifyAll();//   ,      
	}
	
	public static void main(String[] args) {
		Plate plate=new Plate();
		for (int i = 0; i < 200; i++) {//put get   20 
			new getApple(plate).start();
			new putApple(plate).start();
		}
	}
}
//       
class getApple extends Thread{
	private Plate plate;
	public getApple(Plate plate) {
		this.plate=plate;
	}
	@Override
	public void run() {
		try {
			plate.getApple();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

//       
class putApple extends Thread{
	private Plate plate;
	public putApple(Plate plate) {
		this.plate=plate;
	}
	@Override
	public void run() {
		try {
			plate.putApple();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}


以上の列は古典的な生産者/消費者モデルであり、正しくないところがあれば、後で勉強する人を誤解しないように、見た大神が指摘してください.
以下、経典の面接問題のコードを貼ります:大体需要は以下の通りで、ABCの3つのスレッドを開いて、AはAを印刷して、BはBを印刷して、CはCを印刷して、ABCの3つのスレッドは同時に起動して、順番にABCを入力して、このように10回循環します.
/**
 *    /    :    ABCD
 * @author    
 * 2016 6 20 21:00:36
 */
public class PrintABCD {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//      new    ,       new,                   
		PrintABCD abcd=new PrintABCD();
		
		for (int i = 0; i < 10; i++) {
			(new printA(abcd)).start();
			(new printB(abcd)).start();
			(new printC(abcd)).start();
			(new printD(abcd)).start();
		}
	}

	private static int who=1;//   who        ;1,2,3,4    A,B,C,D
	
	public synchronized void printA() throws InterruptedException {
		while (who!=1) {
			this.wait();//   1   ,  1      
		}
		System.out.println("A");
		who=2;//A     B
		this.notifyAll();
	}
	
	public synchronized void printB() throws InterruptedException {
		while (who!=2) {
			this.wait();
		}
		System.out.println("--B");
		who=3;//B     C
		this.notifyAll();
	}
	
	public synchronized void printC() throws InterruptedException {
		while (who!=3) {
			this.wait();
		}
		System.out.println("----C");
		who=4;//C     D
		this.notifyAll();
	}
	
	public synchronized void printD() throws InterruptedException {
		while (who!=4) {
			this.wait();
		}
		System.out.println("------D");
		who=1;//D     A
		this.notifyAll();
	}
}
//    ,    ABCD
class printA extends Thread{
	PrintABCD p;
	printA(PrintABCD p){
		this.p=p;
	}
	@Override
	public void run() {
		try {
			p.printA();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
class printB extends Thread{
	PrintABCD p;
	printB(PrintABCD p){
		this.p=p;
	}
	@Override
	public void run() {
		try {
			p.printB();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
class printC extends Thread{
	PrintABCD p;
	printC(PrintABCD p){
		this.p=p;
	}
	@Override
	public void run() {
		try {
			p.printC();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
class printD extends Thread{
	PrintABCD p;
	printD(PrintABCD p){
		this.p=p;
	}
	@Override
	public void run() {
		try {
			p.printD();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

ここにABCDが印刷されていますが、実はABCと同じですが、demoの中にもう一つ書いてあります.synchronizedキーワードはスレッドの反発を保証します.JDK1.5以前は基本的にsynchronizedキーワードを使用してスレッド間コラボレーションを行っていたが、JDK 1.5以降はより高度な通信方式が提供され、次の記事にdemoコードを貼り付けます.
最后の一言、学は果てしなくて、文章はもし不足するところがあるならば、大神达に直ちに指摘してもらって、私を正して、直ちに后の人を误导することを避けます