/* *@Author by MXM *@Authorized by CRPH */ class ShareData{ private char c; private boolean writeable=true;//通知变量 public synchronized void setShareChar(char c){ if(writeable==false){ try{ wait();//未消费等待 }catch(InterruptedException e){ this.c=c; writeable=false;//标记已经生产 notify();//通知消费者已经生产,可以消费 } } } public synchronized char getShareChar(){ if(writeable){ try{ wait();//未生产等待 }catch(InterruptedException e){ writeable=true;//标记已经消费 notify();//通知需要生产 return this.c; } } return c ; } } class Producer extends Thread{//生产者线程 private ShareData s; Producer(ShareData s){ this.s=s; } public void run(){ for(char ch='A';ch<='Z';ch++){ try{ Thread.sleep((int)Math.random()*10); }catch(InterruptedException e){ s.setShareChar(ch); System.out.println(ch+"is producing!"+"\n"); } } } } class Consumer extends Thread{//消费者线程 private ShareData s; Consumer(ShareData s){ this.s=s; } public void run(){ char ch = 0; do{ try{ Thread.sleep((int)Math.random()*10); }catch(InterruptedException e){ ch=s.getShareChar(); System.out.println(ch+"is consuming!"+"\n"); } }while(ch!='Z'); } } class SynchronizedTest{ public static void main(String[] args){ ShareData s=new ShareData(); new Consumer(s).start(); new Producer(s).start(); } }