执行条件等待时出现异常?
每次使用监视器运行以下多线程代码时,我都会收到异常。
http://pastebin.com/jTGR98W9 http://pastebin.com/hKvuDX2d
每次我执行条件信号时,都会收到一个异常,该异常表示它应该是独占的,但是考虑到它是同步的,它是独占的。或者我做错了什么?
谢谢
I am getting an Exception everytime I run the following multithreaded code with Monitors.
http://pastebin.com/jTGR98W9
http://pastebin.com/hKvuDX2d
Everytime I perform a condition signal, I get an exception, which says that it should be exclusive, it is however exclusive, given that it is synchronized. Or am I doing something wrong?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将旧的固有锁及其信号机制(
synchronized
、wait
和notify
)与新的Lock
混合在一起和Condition
类,尽管它们没有任何关系。这是造成混乱的一个原因,因此我会坚持使用其中之一(最好是Lock
和Condition
)。您的问题可能是由于在调用
signal()
时未持有与条件关联的锁引起的。在方法周围调用lock()
和unlock():
对于其他
synchronized
方法也需要执行相同的操作。You are mixing the old intrinsic locks and their signaling mechanism (
synchronized
,wait
andnotify
) with the newLock
andCondition
classes, though they don't have any relationship. This is a source for confusion, so I'd stick to one of them (preferablyLock
andCondition
).Your problem is probably caused by not holding the lock that is associated to the condition when you are calling
signal()
. Surround the methods with calls tolock()
andunlock():
The same needs to be done for the other
synchronized
methods.