如何等待线程等待
How to wait for thread to wait
我想等到线程进入等待状态。我一直在尝试使用 join(见下文),但它没有用。如何实现?
public Test() {
System.out.print("Started");
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (Test.class) {
try {
Test.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("Done");
}
永远不会打印完成。
您的主线程不会超过 join()
,因为第二个线程永远不会结束。第二个线程永远不会结束,因为它正在等待 Test.class
上的通知,但没有人通知它。
如果你想让你的主线程在第二个线程到达Test.class.wait();
行时继续执行,你需要为这个同步引入另一个监视器。主线程必须在此监视器上等待,第二个线程必须在准备好切换到等待状态时通知它:
System.out.println("Started");
final Object monitor = new Object();
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Finished");
synchronized (monitor) {
monitor.notify(); // Notify the main thread
}
synchronized (Test.class) {
try {
Test.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
synchronized (monitor) {
try {
monitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Done");
这段代码可以工作,尽管它看起来有点难看。它还会留下一个挂起的线程,这将阻止您的程序正常终止。如果你想让这个话题结束,你需要有人打电话给 Test.class.notify()
。
从您的问题中并不清楚您总体上想要达到什么目的。如果你想产生一个线程并等到它完成,你不需要那些 wait()
s 和 notify()
s,只需使用 join()
:
System.out.println("Started");
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Finished");
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Done");
还有一件事:在构造函数中创建和启动线程是一个非常糟糕的主意。为此使用单独的方法(如 initialize()
)。
我想等到线程进入等待状态。我一直在尝试使用 join(见下文),但它没有用。如何实现?
public Test() {
System.out.print("Started");
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (Test.class) {
try {
Test.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("Done");
}
永远不会打印完成。
您的主线程不会超过 join()
,因为第二个线程永远不会结束。第二个线程永远不会结束,因为它正在等待 Test.class
上的通知,但没有人通知它。
如果你想让你的主线程在第二个线程到达Test.class.wait();
行时继续执行,你需要为这个同步引入另一个监视器。主线程必须在此监视器上等待,第二个线程必须在准备好切换到等待状态时通知它:
System.out.println("Started");
final Object monitor = new Object();
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Finished");
synchronized (monitor) {
monitor.notify(); // Notify the main thread
}
synchronized (Test.class) {
try {
Test.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
synchronized (monitor) {
try {
monitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Done");
这段代码可以工作,尽管它看起来有点难看。它还会留下一个挂起的线程,这将阻止您的程序正常终止。如果你想让这个话题结束,你需要有人打电话给 Test.class.notify()
。
从您的问题中并不清楚您总体上想要达到什么目的。如果你想产生一个线程并等到它完成,你不需要那些 wait()
s 和 notify()
s,只需使用 join()
:
System.out.println("Started");
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Finished");
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Done");
还有一件事:在构造函数中创建和启动线程是一个非常糟糕的主意。为此使用单独的方法(如 initialize()
)。