gpt4 book ai didi

java - 调用 Java 对象的 wait() 中断线程同步

转载 作者:搜寻专家 更新时间:2023-10-31 08:24:42 25 4
gpt4 key购买 nike

public class Main2 {
public static void main(String[] args) {
new Test2().start();
new Test2().start();
}
}

class Test2 extends Thread {
@Override
synchronized public void run() {
try {
System.out.println("begin wait");
wait();
} catch (Exception ex) {
}
}
}

作为运行测试的实际结果:开始等待,开始等待,来自两个线程的两次。与预期结果对比:开始等待,只从两个线程之一执行一次,因为在同步的 run() 方法中调用了 wait()。为什么调用 Object 的 wait() 会中断线程同步?

非常感谢!


    public class Main3 {

public static void main(String[] args) {
Test3 t = new Test3();
new Thread(t).start();
new Thread(t).start();
}
}

class Test3 implements Runnable {
synchronized public void run() {
try {
System.out.println("begin wait");
wait();
} catch (Exception ex) {
}
}
}

@akf 和@Sean Owen

感谢您的回复。抱歉我的错误,现在我修改了代码以将同步放在同一个对象的 run() 上,结果仍然是:开始等待,开始等待,两次。

@阿克夫

wait will release the lock that synchronize has grabbed, and will be re-gotten once the thread is notified.

您能详细说明一下吗?

最佳答案

  1. 在此示例中同步的对象不是类,而是实例,因此每个新的 Test2 对象都将在不同的监视器上同步。
  2. 您在这里寻找的方法可能是sleep,而不是waitwait会释放synchronized已经抢到的锁,一旦通知线程会重新获得。

请注意,为了让您的测试正常工作,您需要锁定一个公共(public)对象。如果您想查看 wait 的运行情况,我已经将一个简单的应用程序放在一起,它会弹出一个带有“通知”按钮的框架。将启动两个等待公共(public)对象的线程,并在按下按钮时依次收到通知。

public static void main(String[] args)
{
final Object lock = new Object();

final JFrame frame = new JFrame("Notify Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Notify");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
synchronized(lock) {
lock.notify();
}
}
});
frame.add(button);

SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.setVisible( true );
}
});

new Thread(new Runnable() {
public void run() {
synchronized(lock) {
try {
System.out.println("1. starting");
lock.wait();
System.out.println("1. step 1");
lock.wait();
System.out.println("1. step 2");
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
public void run() {
synchronized(lock) {
try {
System.out.println("2. starting");
lock.wait();
System.out.println("2. step 1");
lock.wait();
System.out.println("2. step 2");
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}).start();

}

对于 wait 的简单解释,JavaDoc 始终是一个很好的起点:

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

关于java - 调用 Java 对象的 wait() 中断线程同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3140423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com