有个有关线程的问题理解不了!

rushfly 2003-09-02 02:35:24
看到一本书上说:

在一个sychronized块中,wait()方法与notify()方法必须同时出现,
为什么?难道不同时出现就不行吗?
...全文
138 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
rushfly 2003-09-03
  • 打赏
  • 举报
回复
我的问题是看《thinking in java》里的话。

哈哈,经过我的长期的思考,终于解决这个问题,上面的朋友所说得比较肤浅,且听我的理解:
----------------------------------------------------------------------------
我的理解如下:
java的wait()与notify()(及其notifyAll)是Object的一个有趣的方法,因为一般的方法只作用于自身,而wait()与notify()却作用于其他的对象----作用于其本身的线程对象。
---为什么wait()与notify()一定要放在synchronized块中呢?
且看下面的定义:
public final void wait() throws IllegalMonitorStateException ,InterruptedException
public final void notify() throws IllegalMonitorStateException
经查IllegalMonitorStateException派生于RuntimeException,意味着我们在程序中不用捕捉(catch)此错误,并且还表明:Wait()与notify()一定要放在synchronized块中,因为这样,线程才能成为该对象的object's monitor,如果不是这样,只要线程调用此方法,就会产生一个运行时错误:IllegalMonitorStateException.
---为什么wait()与notify()或(notifyAll())一定要同时出现呢?
假如同步方法中有wait()而没有notify(),则调用wait()方法的线程则永远沉睡下去,而不会被唤醒,所以有wait()与notify()方法应成对出现,当我想不一定是要在一个synchronized块中的,而只要在对象的所有同步方法中成对出现也可以的。但是在一个synchronized方法块中同时出现wait()与notify()可使代码变得简单,易读。

呵呵:)


相关参考资料:
wait
public final void wait()
throws InterruptedException
Causes 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.

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.


Throws:
IllegalMonitorStateException - if the current thread is not the owner of the object's monitor.
InterruptedException - if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
See Also:
notify(), notifyAll()


notify
public final void notify()
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.

This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:

By executing a synchronized instance method of that object.
By executing the body of a synchronized statement that synchronizes on the object.
For objects of type Class, by executing a synchronized static method of that class.
Only one thread at a time can own an object's monitor.


Throws:
IllegalMonitorStateException - if the current thread is not the owner of this object's monitor.
See Also:
notifyAll(), wait()


wangwd 2003-09-02
  • 打赏
  • 举报
回复
sychronized就是同步,也就是一个锁定的状态,在这种状态下,只有一个块能执行业务操作如果你使用了wait的就必须notify,否则其他的块就不能执行了,程序将一直在锁定的状态,其它的逻辑也运行不了了。
miaoliujun 2003-09-02
  • 打赏
  • 举报
回复
sychronized就是同步,既然被同步了,就是现在只执行这个块,所以假如你有wait的就必须notify,否则其他的就就不能执行了,一直在等待啊
figohl 2003-09-02
  • 打赏
  • 举报
回复
看看OS你就懂了。
asdmonster 2003-09-02
  • 打赏
  • 举报
回复
wait()是等待,notify()是唤醒。你说为什么必须成对出现?
二者倒不是必须的——前提是你有个notifyAll().

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧