/**   输出100遍
 *  奶油>面包    
 * 蛋糕>被买走  如果 是true 表示可以生产 如果是 false 表示可以拿走 不可能生产 如果是true表示可以生产不可以拿走
 */
package a.a.a.a;
class MyThread {
private String firstGoods = "奶油"; //第一件商品
private String secondGoods = "面包"; //第二件商品
private boolean bol = false;
public String getFirstGoods() {
return firstGoods;
}
public void setFirstGoods(String firstGoods) {
this.firstGoods = firstGoods;
}
public String getSecondGoods() {
return secondGoods;
}
public void setSecondGoods(String secondGoods) {
this.secondGoods = secondGoods;
}
public synchronized void set(String firstGoods, String secondGoods){
if(bol == false){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.setFirstGoods(firstGoods );
this.setSecondGoods(secondGoods);
bol = false;
notify();
}
public synchronized void get(){
if(bol = true){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(this.firstGoods+">>>>>>>>>>>>>"+this.secondGoods);
bol = true;
notify();
}
}
class Shop implements Runnable{
private MyThread mt = null;
public Shop(MyThread mt){
this.mt = mt;
}
@Override
public void run(){
for(int i = 0;i<100;i++){
if(i%2==0){
this.mt.set("奶油", "面包");
}else{
this.mt.set("蛋糕", "被买走了");  
}
}
}
}
class Shopping implements Runnable{
private MyThread mt = null;
public Shopping(MyThread mt){
this.mt = mt;
}
@Override
public void run(){
for(int i = 0 ; i<100;i++){
this.mt.get();
}
}
}
public class Synchronized{
public static void main(String[] args) {
MyThread my = new MyThread();
Shop s = new Shop(my);
Shopping sp = new Shopping(my);
new Thread(s).start();
new Thread(sp).start();
}
}