>16 ----------------------------------------------------------------- 【 Ver 】コンソールでjava -versionを実行し、その結果を貼ります。 《必須》 -----------------------------------------------------------------
//Thread.sleep()の例 class A extends Thread { public void run() { for (int i = 0; i < 500; i++) { try { Thread.sleep(10); } catch( InterruptedException ie ) {} System.out.print("★"); } } } class Test { public static void main(String av[]) { A a = new A(); a.start(); for (int i = 0; i < 500; i++) { try { Thread.sleep(10); } catch( InterruptedException ie ) {} System.out.print("●"); } } }
18 名前:17 mailto:sage [2007/01/23(火) 09:23:51 ]
>16 // synchronizedブロックの例 class Shared {} class A extends Thread { Shared s; public A(Shared s) { this.s = s; } public void run() { for (int i = 0; i < 500; i++) { synchronized (s) { s.notify(); System.out.print("★"); try { s.wait(); } catch (InterruptedException ie) {} } } } } class Test { public static void main(String av[]) { Shared s = new Shared(); A a = new A(s); a.start(); try { Thread.sleep(10); } catch (InterruptedException ie) {} for (int i = 0; i < 500; i++) { synchronized (s) { System.out.print("●"); s.notify(); try { s.wait(100); } catch( InterruptedException ie ) {} } } } }