首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行线程2线程,等待并通知

运行线程2线程,等待并通知
EN

Stack Overflow用户
提问于 2017-06-26 08:20:22
回答 2查看 644关注 0票数 0

我对Java多线程编程很陌生.在这里,我想通过运行线程1运行两个线程,等待(),通知(),然后线程2,等待(),通知(),像wise一样。

有人能帮我实现预期的产出吗?

代码语言:javascript
复制
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();
   }   
}

预期输出为

代码语言:javascript
复制
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

当前输出除外为

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-26 09:35:03

您应该在同一个对象上使用wait()notify()进行通信。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2017-06-26 08:34:03

你在等待/通知错了。

为了通知另一个线程,您必须在上调用notify (另一个对象)。

您的代码所做的是:线程将自己放入等待中..。晚些时候通知自己。

在这个意义上:后退一步,阅读如何正确使用这两种方法;例如,开始阅读here

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44755566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档