cuiyi's blog(崔毅 crazycy)

记录点滴 鉴往事之得失 以资于发展
数据加载中……

线程中的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()方法时,需要把它放到一个同步段里,即应该在调用前使用
1synchroed(this){
2  thread.wait();
3  
4}

5

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


Thanks very much to visit blog,  welcome your feedback,  your feedback is the Driver && Power to me

posted on 2006-10-08 19:21 crazycy 阅读(13650) 评论(24)  编辑  收藏 所属分类: JavaSE语言

评论

# re: 线程中的current thread not owner异常错误  回复  更多评论   

您的内容对我有帮助,谢谢!
2007-05-30 14:22 | shaoxiongwang

# re: 线程中的current thread not owner异常错误  回复  更多评论   

thak you !
2007-07-12 15:14 | netiger

# re: 线程中的current thread not owner异常错误  回复  更多评论   

谢谢^_^
2007-08-01 17:17 | txd

# re: 线程中的current thread not owner异常错误  回复  更多评论   

3ks
2008-01-21 15:59 | ronchie

# re: 线程中的current thread not owner异常错误  回复  更多评论   

ths
2008-03-23 20:48 | er

# re: 线程中的current thread not owner异常错误  回复  更多评论   

谢谢啦
2008-03-31 16:46 | hxfein

# re: 线程中的current thread not owner异常错误  回复  更多评论   

Once I was given an interview in IBM, the program is just to write interaction between productor and consumer, I met the problem; and, I solved at that time, but sadly I cannot give the explation about this.
2008-03-31 22:13 | crazycy

# re: 线程中的current thread not owner异常错误  回复  更多评论   

非常感谢!受益匪浅!我在用友面试时就遇到这个问题!但是没有答上来!
2008-04-16 18:05 | zhanggl

# re: 线程中的current thread not owner异常错误  回复  更多评论   

Thanks! It works!
2008-08-01 12:14 | Shengkai Zhu

# re: 线程中的current thread not owner异常错误  回复  更多评论   

谢谢!
2008-10-08 13:23 | freemelody

# re: 线程中的current thread not owner异常错误  回复  更多评论   

感谢
2008-11-11 10:11 | 筋抽的狗狗

# re: 线程中的current thread not owner异常错误  回复  更多评论   

e感谢,根据您的帮助,我解决了此异常。
2008-11-20 13:14 | autumn

# re: 线程中的current thread not owner异常错误  回复  更多评论   

Anyway, you are best.
2009-03-23 09:43 | IMGM

# re: 线程中的current thread not owner异常错误  回复  更多评论   

thanks you!
it is very helpfull
2009-06-10 18:03 | duanhw

# re: 线程中的current thread not owner异常错误  回复  更多评论   

thank you
2009-06-19 09:49 | mouse

# re: 线程中的current thread not owner异常错误  回复  更多评论   

只有在当前线程中才能使用???
public class TaskMonitor extends Thread {
private CreateTask create = null;
public TaskMonitor(CreateTask create){
this.create = create;
}
//任务监控,每隔2妙,通知CreateTask创造1条任务放到队列中
public void run() {
while(true){
try {
Thread.sleep(2000);
System.out.println("TaskMonitor");
synchronized (create) {
create.notify();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
其中的create就是另一个线程的对象
2009-06-22 13:49 | 乞丐

# re: 线程中的current thread not owner异常错误  回复  更多评论   

Thank you.
2009-09-16 16:59 | winstone

# re: 线程中的current thread not owner异常错误  回复  更多评论   

good!!!
2009-11-22 10:17 | missme

# re: 线程中的current thread not owner异常错误  回复  更多评论   

谢谢你的分析,但是还不是很详细,如果能再详细一点就好了。
2010-02-04 23:18 | netant83

# re: 线程中的current thread not owner异常错误[未登录]  回复  更多评论   

谢谢!
2010-03-20 15:20 | 1

# re: 线程中的current thread not owner异常错误[未登录]  回复  更多评论   

你说的不对,两个线程共用一个锁,是可以的。
public A extends Thread{
public static Object lock = new Object();
public void run(){
synchronized(lock){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public B extends Thread{
public void run(){
Object lock = A.lock;
synchronized(lock){
lock.notify();
}
}
}
2010-07-02 12:24 | tony

# re: 线程中的current thread not owner异常错误  回复  更多评论   

很好的内容,有帮助
2010-07-26 10:52 | wharry

# re: 线程中的current thread not owner异常错误[未登录]  回复  更多评论   

你说的很好,对我很有用!非常感谢!
2011-04-02 23:08 | solo

# re: 线程中的current thread not owner异常错误  回复  更多评论   

很有用
2015-09-10 14:32 | 11

只有注册用户登录后才能发表评论。


网站导航: