class CoThread extends Thread{ private Waiting w; private int id; private Counter c; CoThread(int id, Counter c){ super(); w=new Waiting(); this.id = id; this.c = c; } public void run(){ for(int i=0; i<10; i++){ w.wait(this); System.out.print(id+" "); synchronized(c){ int v=c.get(); w.wait(this); c.set(v+1); } } c.show(); } public static void main(String[] args){ Counter c=new Counter(); CoThread m; m=new CoThread(0, c); m.start(); m=new CoThread(1, c); m.start(); m=new CoThread(2, c); m.start(); } }問題解決は runメソッド中の, synchronized(c){....} にある.
$Id: toc.html,v 1.1 2000-06-25 23:28:56+09 kaiya Exp kaiya $