簡體   English   中英

等待運行線程2線程並通知

[英]Run Thread 2 threads with wait and notify

我是Java多線程編程的新手。 在這里,我想通過運行線程1 wait()notift()然后運行線程2 wait()notify()來運行兩個線程。

有人可以幫助我實現下面給出的預期輸出

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)

您正在等待/通知錯誤。

為了通知另一個線程,您必須在另一個對象上調用notify

您的代碼所做的是:線程將自己置於等待狀態...以便稍后進行通知。

從這個意義上講:退后一步,了解如何正確使用這兩種方法; 例如,從這里開始閱讀。

您應該在同一對象上使用wait()notify()進行通信。

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM