简单的Java并发问题
线程 1:
if(!conditionFullfiled) this.wait();
线程 2:
if(conditionFullfiled) thread1.notify();
当某些条件满足时,我想从线程 2 唤醒线程 1。但是,当 thread1.notify()
被调用 if(!conditionFullfiled) ***HERE*** this.wait();
时,难道没有问题吗?
Thread 1:
if(!conditionFullfiled) this.wait();
Thread 2:
if(conditionFullfiled) thread1.notify();
I want to wake up thread 1 from thread 2, when some condition is fullfiled. But isn't there a problem, when thread1.notify()
is called if(!conditionFullfiled) ***HERE*** this.wait();
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要执行 obj.wait() 和 obj.notify(),您需要拥有要等待/通知的对象的监视器。在您的代码中,您可能不需要 thread1.notify()。示例:
Thread1:
Thread2:
synchronized
锁位于someSharedObject
(可以是this
)上,这意味着两个线程永远不会发生冲突。.wait()
释放当前持有的监视器,因此当Thread1等待时,Thread2不会被阻塞。编辑:我了解了一些有关虚假唤醒的知识。
.wait()
必须在while
循环中完成 -if
还不够。 为什么线程会自发地从 wait() 中唤醒? 。感谢円野盐治教我。编辑:澄清
.wait()
释放监视器。To do
obj.wait()
andobj.notify()
, you need to own the monitor of the object you're going to wait/notify on. In your code, you probably don't want thread1.notify(). Example:Thread1:
Thread2:
The
synchronized
lock is onsomeSharedObject
(can bethis
), which means the two threads will never clash..wait()
releases the currently held monitor, so Thread2 will not be blocked when Thread1 is waiting.Edit: I learnt something about spurious wake ups. The
.wait()
must be done in awhile
loop -if
is not enough. Why do threads spontaneously awake from wait()? . Thanks Enno Shioji for teaching me.Edit: Clarified
.wait()
releases monitor.你这里有两个问题。
你不应该在线程对象本身上调用wait()和notify()。更好的方法是使用特殊的锁对象,例如
私有对象锁 = new Object();
......
lock.wait();
下一个问题是你必须将 wait() 和 notification 调用到同步块中,即
同步(锁定){
// 一些代码
锁.wait();
下一个
在代码的其他地方说:
将
wait()
和notify()
包装方法本地化在一个类中很方便,因此它们可以访问锁定对象。欲了解更多信息,请阅读
http://download.oracle.com/javase/6/docs/api/
http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html
You have 2 problems here.
you should not call wait() and notify() on thread object itself. Better way to do this is to use special lock object, e.g.
private Object lock = new Object();
......
lock.wait();
The next problem is that you have to call both wait() and notify into synchornized block, i.e.
syncronized(lock) {
// some code
lock.wait();
}
then in other place in code say:
It is convenient to localize both
wait()
andnotify()
wrapping methods in one class, so they have access to lock object.For more information read
http://download.oracle.com/javase/6/docs/api/
http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html
完全没有问题,因为等待释放了对象锁(如果是这种情况)。
最佳实践是在 while 块中保护等待/通知条件 - 以避免虚假唤醒。
No problem at all since wait releases object lock (in case this).
It's a best practice to guard wait/notify conditions in while blocks - to avoid spurious wake-ups.
您使用什么对象作为“this”?如果您在 thread1 对象上调用 wait(),并且您显示的两个语句都包含在如下循环中:
然后你的代码就会按照你想要的方式工作。 (当条件为假时,第一个线程将停止,否则工作)。
诀窍在于线程对象上的交互是同步的,因此一个线程在其他线程处理该对象时不能中断。
编辑:
如果您不在线程上同步,而是在其他对象上同步(您可以简单地创建纯对象来提供锁),那就更好了。
What object you use as "this"? If you invoke wait() on thread1 object, and both statements you have shown are wrapped in a loop like this:
Then your code will work as you want. (First thread will halt when condition is false, and work otherwise).
The trick is that interactions on the thread object are synchronized, so one thread can not interrupt while other is working with the object.
EDIT:
It will be even better if you will synchronize not on the thread, but on some other object (you can simply create pure object to provide locks).