在Java中同步2个线程的更简单方法?
我不想确定主线程中的某些代码将在辅助线程中执行某些代码之后执行。这就是我得到的:
final Object lock = new Object();
final Thread t = new Thread(new Runnable() {
public void run() {
synchronized(lock) {
System.out.println("qwerty");
lock.notify();
}
}
});
synchronized(lock) {
t.start();
lock.wait();
}
System.out.println("absolutely sure, qwerty is above");
- 这是正确的解决方案吗?
- 还有更短的方法可以做到同样的事情吗?
I wan't to be sure that some piece of my code within the main thread will be executed after some piece of code executed withing the secondary thread. Here's what I got:
final Object lock = new Object();
final Thread t = new Thread(new Runnable() {
public void run() {
synchronized(lock) {
System.out.println("qwerty");
lock.notify();
}
}
});
synchronized(lock) {
t.start();
lock.wait();
}
System.out.println("absolutely sure, qwerty is above");
- Is it correct solution?
- Any shorter ways to do the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像 notify() 和 wait() 这样的东西实际上是低级同步原语。
您应该尽可能使用更高级别的抽象,例如在本例中,CountDownLatch。
以下只是一个应该帮助您入门的示例(例如,此处未考虑超时问题):
诸如 wait 和 notify 之类的低级事物实际上是只是您不应该关心的低级 Java 特性(除非您正在编写并发 API)。
此外,我建议阅读一本精彩的书:Java Concurrency In Practice。
Things like notify() and wait() are really low-level synchronization primitives.
You should use higher-level abstractions whenever possible, like in this case, say, a CountDownLatch.
The following is just an example that should get you started (for example the timeout issues aren't taken into account here):
Low-level things like wait and notify are really just low-level Java idiosynchrasies that you shouldn't be concerned with (unless you're writing concurrency APIs).
Additionally, I'd suggest reading the amazing book: Java Concurrency In Practice.
假设您的主线程需要启动辅助线程的处理,则 Exchanger 将是最简单的解决方案。
如果辅助线程是独立的,则某种形式的 BlockingQueue 是合适的。
使用 Exchanger 的示例(具有适当的异常处理)。一个交换器可以用两个队列代替。
Assuming that your main thread needs to initiate the secondary thread's processing, an Exchanger would be the simplest solution.
If the secondary thread is independent, then some form of BlockingQueue would be appropriate.
An example using Exchanger (with proper exception handling). The one Exchanger could be substituted with two queues.
您给出的代码的最佳解决方案是:
不需要时不要使用线程。
但是,如果您知道自己在使用线程做什么,那么@DoubleMalt 的解决方案就是正确的选择(
t.join()
)。您可能还会发现值得一读 http://download.oracle.com/javase /教程/基本/并发/
The best solution for the code you have given is:
Don't use threading when you don't need it.
But if you know what you're doing with the thread, then @DoubleMalt's solution is the way to go (
t.join()
).You'd also probably find it worth reading http://download.oracle.com/javase/tutorial/essential/concurrency/