线程中的current thread not owner异常错误

多线程常用的一些方法: wait(),wait(long),notify(),notifyAll()等

这些方法是当前类的实例方法,

wait()      是使持有对象锁的线程释放锁;
wait(long)是使持有对象锁的线程释放锁时间为long(毫秒)后,再次获得锁,wait()和wait(0)等价;
notify()    是唤醒一个正在等待该对象锁的线程,如果等待的线程不止一个,那么被唤醒的线程由jvm确定;
notifyAll 是唤醒所有正在等待该对象锁的线程.

并且应该优先使用notifyAll()方法,因为唤醒所有线程比唤醒一个线程更容易让jvm找到最适合被唤醒的线程.

对于上述方法,只有在当前线程中才能使用,否则报运行时错误java.lang.IllegalMonitorStateException: current thread not owner.

从实现角度来分析:
在线程调用wait()方法时,需要把它放到一个同步段里,即应该在调用前使用
1线程中的current thread not owner异常错误 - 叫哥就行 - 叫哥就行的博客线程中的current thread not owner异常错误 - 叫哥就行 - 叫哥就行的博客synchroed(this)线程中的current thread not owner异常错误 - 叫哥就行 - 叫哥就行的博客{
2线程中的current thread not owner异常错误 - 叫哥就行 - 叫哥就行的博客   thread.wait();
3线程中的current thread not owner异常错误 - 叫哥就行 - 叫哥就行的博客  线程中的current thread not owner异常错误 - 叫哥就行 - 叫哥就行的博客线程中的current thread not owner异常错误 - 叫哥就行 - 叫哥就行的博客
4线程中的current thread not owner异常错误 - 叫哥就行 - 叫哥就行的博客}
5线程中的current thread not owner异常错误 - 叫哥就行 - 叫哥就行的博客


否则将会出现"java.lang.IllegalMonitorStateException: current thread not owner"的异常。

转自 : http://www.blogjava.net/crazycy/archive/2006/10/08/73896.html

原文地址:https://www.cnblogs.com/winstonet/p/6916290.html