本文介绍了从Java的main()内部在线程实例上运行wait()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用java.lang.Object中的wait()的定时版本,并观察到它在两种不同的情况下的行为不同.

I am playing around with the timed version of wait() in java.lang.Object and have observed that it acts differently in two different scenarios.

方案1:在线程中使用run()的默认定义

public static void main (String[] args) throws InterruptedException {
    Thread t = new Thread();
    t.start();
    System.out.print("X");
    synchronized(t) { t.wait(10000);}
    System.out.print("Y");
}

方案1的问题:我在X和Y之间遇到延迟.这是因为我正在从main调用wait()(即使在t上),因此要调用main的调用堆栈线程正在使用,而不是第二个线程?

Questions on scenario1: I am experiencing a delay between X and Y. Is this because I am calling wait() from main (even though, on t) and therefore the call stack of the main thread is being used, rather than that of the second thread?

场景2: 动态子类化线程以覆盖run()以便打印内容.

public static void main (String[] args) throws InterruptedException {
     Thread t = new Thread() {public void run()
                     {System.out.print("I am the second thread.");}};
     t.start();
     System.out.print("X");
     synchronized(t) { t.wait(10000);}
     System.out.print("Y");
}

关于方案2的问题:我完全没有遇到任何延迟!仅仅因为我重写了run(),发生了什么变化?现在,每次我运行程序时,它都会立即打印"XI是第二个线程.Y",而不会出现任何延迟!

Questions on scenario2: I am NOT experiencing any delay at all! What has changed just because I have overridden run()? Now, each time I run the program it immediately prints "XI am the second thread.Y" without any delay, whatsoever! Where has the effect of wait() gone?

推荐答案

关于线程完成方式如何发送notifyAll的 >是相关且正确的,请向我+1.我将尝试添加一些有关为何如此重要的信息.

The explanation about how the thread finishing sends a notifyAll is relevant and correct, +1 from me. I'll try to add some information about why this is relevant.

致电时

synchronized(t) { t.wait(10000);}

在主线程中,是等待执行的主线程. t是主线程正在等待的监视器.您期望这会使您的t线程进入休眠状态,这是错误的.

in the main thread, it is the main thread that does the waiting. t is the monitor that the main thread is waiting on. Your expectation that this should make your t thread go dormant is mistaken.

此处的监视器(共享对象被锁定,恰好是t)用于在不同线程之间进行通信,一个线程在监视器上调用notifyAll,而其他在监视器上等待的线程接收该通知.您可以将显示器视为共享的通讯点.

The monitor here (the shared object being locked on, which happens to be t) is used to communicate between the different threads, a thread calls notifyAll on the monitor and the other threads waiting on the monitor receive the notification. You can think of the monitor as a shared communication point.

在您的第一个示例中,线程t开始并立即结束(因为它没有任何关系).在主线程开始等待之前,该线程完成并发送其通知,因此您会看到一个延迟,直到等待超时.

In your first example, the thread t starts and finishes immediately (because it doesn't have anything to do). The thread finishes and sends its notification before the main thread starts waiting, so you see a delay until the wait times out.

在第二个示例中,线程t可以打印一些内容,它与主线程之间存在竞争条件.这是万能的,首先发生的事情取决于时间安排的偶然性.您所看到的是,线程t现在必须在控制台上打印一行,因此它设法保持繁忙的时间足够长,以便在主线程开始等待时它仍处于活动状态,从而允许主线程接收t结束时通知,导致主线程缩短其等待时间.

In the second example, the thread t has something to print, there's a race condition between it and the main thread. It's a free-for-all, what happens first depends on accidents of timing. What you're seeing is that the thread t now has to print a line to the console, so it manages to keep busy long enough that it's still alive at the time the main thread starts to wait, allowing the main thread to receive the notification when t finishes, causing the main thread to cut its wait short.

这篇关于从Java的main()内部在线程实例上运行wait()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!