【问题标题】:Does wait always need notify to work?等待总是需要通知才能工作吗?
【发布时间】:2016-12-08 02:49:48
【问题描述】:

直到现在我都知道等待总是需要通知才能正常工作。但是当尝试下面的代码时,我对等待和通知的工作有点困惑。我创建了三个线程 t1、t2、t3 并分别传递了可运行的 T1、T2 和 T3。据我说,当我启动三个线程时,只有 t1 应该打印,t2 和 t3 应该进入等待状态并继续等待,因为没有人是通知。

但是 o/p 对我来说是不可预测的。有人可以解释一下吗。下面是我的课程。

package com.vikash.Threading;

class T1 implements Runnable {

    private State state;

    public T1(State state) {
        this.state=state;
    }

    @Override
    public void run() {

        synchronized (state) {
            while(state.getState()!=1) {
                try {
                    state.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            synchronized (state) {
                System.out.println(Thread.currentThread().getName());
                state.setState(2);
            }
        }
    }
}

class T2 implements Runnable {

    private State state;

    public T2(State state) {
        this.state=state;
    }

    @Override
    public void run() {

        synchronized (state) {
            while(state.getState()!=2) {
                try {
                    state.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            synchronized (state) {
                System.out.println(Thread.currentThread().getName());
                state.setState(3);
            }
        }
    }
}

class T3 implements Runnable {

    private State state;

    public T3(State state) {
        this.state=state;
    }

    @Override
    public void run() {

        synchronized (state) {
            while(state.getState()!=3) {
                try {
                    state.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            synchronized (state) {
                System.out.println(Thread.currentThread().getName());
                state.setState(1);
            }
        }
    }
}

public class Sequence {

    public static void main(String[] args) {

        State state=new State();
        Thread t1=new Thread(new T1(state),"First");
        Thread t2=new Thread(new T2(state),"Second");
        Thread t3=new Thread(new T3(state),"Third");
        t1.start();
        t2.start();
        t3.start();
    }
}



package com.vikash.Threading;

public class State {

    private int state=1;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }
}

根据评论,我正在修改我的问题。o/p 有时我得到第一秒但它不会终止,有时是第一秒第三和终止。

【问题讨论】:

标签: java multithreading wait notify


【解决方案1】:

您的期望不正确,您的所有线程都可能打印并在您的程序当前编写时结束(但这取决于随机机会)

这取决于哪个线程首先使用它们都拥有的synchronized 块来获取state 上的监视器。

考虑这个流程:

  1. T1 首先进入synchronized (state) 块。 T2 和 T3 正在等待进入他们的synchronized (state) 区块。
  2. T1 不会像 state.getState() == 1 那样等待,而是
  3. T1 打印线程名称并将 2 分配给 state.state
  4. T1 退出同步块
  5. T2 或 T3 输入它们的 synchronized (state) 块,假设它是 T2(先进入哪个是 Java 中未定义的行为,可能是随机的)
  6. 所以 T2 不会像 state.getState() == 2 那样等待
  7. T2 打印线程名称并将 3 分配给 state.state
  8. T2 退出同步块
  9. T3进入同步块,不等待,打印线程名
  10. 程序完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    相关资源
    最近更新 更多