2017-06-26 8 views
0

私はJavaマルチスレッドプログラミングの新人です。ここで私は2つのスレッドを実行したい run thread 1 wait()notift()そしてスレッド2 wait()notify()like wise。待機スレッドでスレッド2を実行して通知する

誰かが

コードの下に与えられている期待される出力を達成するために私を助けることができる

class RunnableDemo implements Runnable { 
    private Thread t; 
    private String threadName; 

    RunnableDemo(String name) { 
     threadName = name; 
     System.out.println("Creating " + threadName); 
    } 

    public void run() { 
     System.out.println("Running " + threadName); 
    synchronized (t){ 
     try { 
     for(int i = 1; i < = 5; i--) { 
      System.out.println("Thread: " + threadName + ", " + i); 
      wait(); 
      notify(); 
     } 
     }catch (InterruptedException e) { 
     System.out.println("Thread " + threadName + " interrupted."); 
     } 
    } 
     System.out.println("Thread " + threadName + " exiting."); 
    } 

    public void start() { 
     System.out.println("Starting " + threadName); 
     if (t == null) { 
     t = new Thread (this, threadName); 
     t.start(); 
     } 
    } 
} 

public class TestThread { 

    public static void main(String args[]) { 
     RunnableDemo R1 = new RunnableDemo("Thread-1"); 
     R1.start(); 

     RunnableDemo R2 = new RunnableDemo("Thread-2"); 
     R2.start(); 
    } 
} 

の予想される出力は例外と

Creating Thread-1 
Starting Thread-1 
Creating Thread-2 
Starting Thread-2 
Running Thread-1 
Thread: Thread-1, 1 
Running Thread-2 
Thread: Thread-2, 1 
............ 
Running Thread-1 
Thread: Thread-1, 5 
Running Thread-2 
Thread: Thread-2, 5 

電流出力がです

Creating Thread-1 
Starting Thread-1 
Creating Thread-2 
Starting Thread-2 
Running Thread-1 
Thread: Thread-1, 1 
Running Thread-2 
Thread: Thread-2, 1 
Exception in thread "Thread-1" Exception in thread "Thread-2" java.lang.IllegalMonitorStateException 
    at java.lang.Object.wait(Native Method) 
    at java.lang.Object.wait(Object.java:502) 
    at RunnableDemo.run(TestThread.java:16) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalMonitorStateException 
    at java.lang.Object.wait(Native Method) 
    at java.lang.Object.wait(Object.java:502) 
    at RunnableDemo.run(TestThread.java:16) 
    at java.lang.Thread.run(Thread.java:745) 
+0

[ここ](http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html) –

+0

@KevinAndersonので、私の期待される結果を達成するためにとにかくがあり –

+0

おそらくそうだ。スレッドの使い方を学んだら、それは可能でなければなりません。 –

答えて

0

同じオブジェクトには、wait()notify()を使用して通信する必要があります。

Javaスレッド上に読む
class RunnableDemo implements Runnable { 
    private ThreadMonitor lock; 
    private String threadName; 
    private String otheThreadName; 

    RunnableDemo(String name, ThreadMonitor lock, String otheThreadName) { 
     this.threadName = name; 
     this.lock = lock; 
     this.otheThreadName = otheThreadName; 
     System.out.println("Creating " + threadName); 
    } 

    public void run() { 

     synchronized (lock) { 
      try { 

       for (int i = 1; i <= 5; i++) { 
        while (!lock.getRunningThread().equals(threadName)) { 
         lock.wait(); 
        } 
        System.out.println("Running " + threadName); 
        System.out.println("Thread: " + threadName + ", " + i); 
        lock.setRunningThread(otheThreadName); 
        lock.notify(); 
       } 
      } catch (InterruptedException e) { 
       System.out.println("Thread " + threadName + " interrupted."); 
      } 
     } 
     System.out.println("Thread " + threadName + " exiting."); 
    } 

    public void start() { 
     System.out.println("Starting " + threadName); 

     Thread t = new Thread(this, threadName); 
     t.start(); 
    } 

} 

public class TestThread { 

    public static void main(String args[]) { 
     ThreadMonitor lock = new ThreadMonitor("Thread-1"); 
     RunnableDemo R1 = new RunnableDemo("Thread-1", lock, "Thread-2"); 
     R1.start(); 

     RunnableDemo R2 = new RunnableDemo("Thread-2", lock, "Thread-1"); 
     R2.start(); 
    } 
} 

class ThreadMonitor { 
    private String runningThread; 

    public ThreadMonitor(String runningThread) { 
     this.runningThread = runningThread; 
    } 

    public String getRunningThread() { 
     return runningThread; 
    } 

    public void setRunningThread(String threadName) { 
     runningThread = threadName; 
    } 
} 
0

間違っていると通知されています。 するためには

別のスレッドに通知し、あなたは別のオブジェクトを通知を呼び出す必要があります。

あなたのコードは何ですか:スレッドが自分自身を待機状態にして後で通知します。

この意味で:ステップバックして、どのようにを正しく読む方法これらの2つの方法を使用します。たとえば、hereを読み始めます。

関連する問題