当前位置: 技术问答>java相关
有关java线程方面的问题,请高手帮忙
来源: 互联网 发布时间:2015-05-10
本文导语: 下述错误是什么原因? java.lang.IllegalMonitorStateException: current thread not owner at java.lang.Object.notify(Native Method) at Downloader.resumeDownload(Downloader.java:194) at DownloadManager$4.actionPerformed(Down...
下述错误是什么原因?
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notify(Native Method)
at Downloader.resumeDownload(Downloader.java:194)
at DownloadManager$4.actionPerformed(DownloadManager.java:122)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:17
67)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Abstra
ctButton.java:1820)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:419)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:258)
at java.awt.Component.processMouseEvent(Component.java:5021)
at java.awt.Component.processEvent(Component.java:4818)
at java.awt.Container.processEvent(Container.java:1380)
at java.awt.Component.dispatchEventImpl(Component.java:3526)
at java.awt.Container.dispatchEventImpl(Container.java:1437)
at java.awt.Component.dispatchEvent(Component.java:3367)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3214
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2929)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2859)
at java.awt.Container.dispatchEventImpl(Container.java:1423)
at java.awt.Window.dispatchEventImpl(Window.java:1566)
at java.awt.Component.dispatchEvent(Component.java:3367)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:190)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:130)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)
我感觉是线程不对,
这是一个下载器,在暂停的代码里面,
//暂停按钮的事件定义
suspendButton = new JButton("Suspend");
suspendButton.setEnabled(false);
suspendButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
suspendButton.setEnabled(false);
resumeButton.setEnabled(true);
stopButton.setEnabled(true);
downloader.setSuspended(true);
}
});
innerPanel.add(suspendButton);
//恢复继续按钮的事件定义
resumeButton = new JButton("Resume");
resumeButton.setEnabled(false);
resumeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
resumeButton.setEnabled(false);
suspendButton.setEnabled(true);
stopButton.setEnabled(true);
downloader.resumeDownload();
}
});
innerPanel.add(resumeButton);
//下面是恢复继续按钮的事件。
public void resumeDownload(){
//setSuspended(false);
this.notify();
}
//下面是总处理(判断当前状态的)里面的片断
/*
* For DownloadManager.class
*/
synchronized(this){
if(isSuspended()){
try{
this.wait();
setSuspended(false);
}catch (InterruptedException ie){
setStopped(true);
break;
}
}
}
可能是this引起的,我不清楚有关线程的问题,刚接触啊!
请各位帮帮忙。
如果需要源码请留下mail。
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notify(Native Method)
at Downloader.resumeDownload(Downloader.java:194)
at DownloadManager$4.actionPerformed(DownloadManager.java:122)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:17
67)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Abstra
ctButton.java:1820)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:419)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:258)
at java.awt.Component.processMouseEvent(Component.java:5021)
at java.awt.Component.processEvent(Component.java:4818)
at java.awt.Container.processEvent(Container.java:1380)
at java.awt.Component.dispatchEventImpl(Component.java:3526)
at java.awt.Container.dispatchEventImpl(Container.java:1437)
at java.awt.Component.dispatchEvent(Component.java:3367)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3214
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2929)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2859)
at java.awt.Container.dispatchEventImpl(Container.java:1423)
at java.awt.Window.dispatchEventImpl(Window.java:1566)
at java.awt.Component.dispatchEvent(Component.java:3367)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:190)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:130)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)
我感觉是线程不对,
这是一个下载器,在暂停的代码里面,
//暂停按钮的事件定义
suspendButton = new JButton("Suspend");
suspendButton.setEnabled(false);
suspendButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
suspendButton.setEnabled(false);
resumeButton.setEnabled(true);
stopButton.setEnabled(true);
downloader.setSuspended(true);
}
});
innerPanel.add(suspendButton);
//恢复继续按钮的事件定义
resumeButton = new JButton("Resume");
resumeButton.setEnabled(false);
resumeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
resumeButton.setEnabled(false);
suspendButton.setEnabled(true);
stopButton.setEnabled(true);
downloader.resumeDownload();
}
});
innerPanel.add(resumeButton);
//下面是恢复继续按钮的事件。
public void resumeDownload(){
//setSuspended(false);
this.notify();
}
//下面是总处理(判断当前状态的)里面的片断
/*
* For DownloadManager.class
*/
synchronized(this){
if(isSuspended()){
try{
this.wait();
setSuspended(false);
}catch (InterruptedException ie){
setStopped(true);
break;
}
}
}
可能是this引起的,我不清楚有关线程的问题,刚接触啊!
请各位帮帮忙。
如果需要源码请留下mail。
|
直接调用object.notify()or wait()等方法便会抛出如上异常。
要挂起或唤醒线程可参照如下方法:
假设存在线程threadA,方法funcA();要在threadA调用funcA()时让threadA等待,funcA()可这样定义:
void funcA() {
wait();
}
这样,谁调用funcA()谁便会挂起。
notify(),notifyAll()也是如此使用。
//广告
//oop.51.net上面有我写的吃豆子源码下载。
//采用了多线程,有挂起、唤醒等此类代码。
要挂起或唤醒线程可参照如下方法:
假设存在线程threadA,方法funcA();要在threadA调用funcA()时让threadA等待,funcA()可这样定义:
void funcA() {
wait();
}
这样,谁调用funcA()谁便会挂起。
notify(),notifyAll()也是如此使用。
//广告
//oop.51.net上面有我写的吃豆子源码下载。
//采用了多线程,有挂起、唤醒等此类代码。
|
不了解你的源代码,试试下面:
public synchronized void resumeDownload(){
if(!isSuspended()){
this.notifyAll();
setSuspended(true);
}
}
public synchronized void resumeDownload(){
if(!isSuspended()){
this.notifyAll();
setSuspended(true);
}
}
|
我感觉不一定是向Object类,而是始终向上扩展,最终到达错误的根源
比如OutofBounds异常,
如果你用数组超界了,就会出现这种异常,它在Array中抛掷的
所以首先出现Array类的Index OutofBounds
然后到你自己定义的类中YourClass.CallArrayMethod(调用Array的函数)
最后才到你写的出错的那句话,比如array[100]不存在的一个
比如OutofBounds异常,
如果你用数组超界了,就会出现这种异常,它在Array中抛掷的
所以首先出现Array类的Index OutofBounds
然后到你自己定义的类中YourClass.CallArrayMethod(调用Array的函数)
最后才到你写的出错的那句话,比如array[100]不存在的一个
|
wait(), notify(), notifyAll()
必须在同步块中才能调用。否则线程就没有Moniter owner,必然会出IllegalMonitorStateException。
只有被同步的程序块才有锁,有锁才有Moniter owner,才能wait(),否则没有意义。
必须在同步块中才能调用。否则线程就没有Moniter owner,必然会出IllegalMonitorStateException。
只有被同步的程序块才有锁,有锁才有Moniter owner,才能wait(),否则没有意义。