java
1public class Main { 2 public static void main(String[] args) throws InterruptedException { 3 // TODO Auto-generated method stub 4 ThreadProcess th1 = new ThreadProcess(1); 5 th1.start(); 6 Thread.sleep(100); 7 ThreadProcess th2 = new ThreadProcess(2); 8 th2.start(); 9 } 10 11}
java
1public class ThreadProcess extends Thread{ 2 private int num=0; 3 public ThreadProcess(int num) { 4 this.num = num; 5 } 6 public synchronized void run() { 7 try { 8 System.out.println("run start num:"+this.num); 9 10 if(num == 1) { 11 //gate.getRrequest(); 12 System.out.println("wait"); 13 wait(); 14 System.out.println("wait解除"); 15 }else { 16 //gate.putRequest(); 17 System.out.println("notifyAll"); 18 notifyAll(); 19 System.out.println("notifyAll完了"); 20 } 21 22 } catch (InterruptedException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } 26 } 27}
###行いたいこと
①Mainクラスより、スレッド1(num=1)を起動
②スレッド1(num=1)をwaitを呼び出すことにより停止します
③時間を開けて、スレッド2(num=2)を起動
④スレッド2(num=2)にてnotifyAllを呼び出し、停止しているスレッド1(num=1)を復帰
ログとしては以下のようになってほしいです
run start num:1
wait
run start num:2
notifyAll
notifyAll完了
wait解除
###現状
run start num:1
wait
run start num:2
notifyAll
notifyAll完了
しかし、現状としては
wait解除
のログが表示されず、プログラムも終了されません。
どのようにすれば、notifyAllにて停止したスレッドを復帰させることが出来るのでしょうか。
どなたかご教示頂けないでしょうか。
どうぞよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/11 11:49