gpt4 book ai didi

Java - 无法通知等待线程?

转载 作者:行者123 更新时间:2023-12-01 19:02:49 25 4
gpt4 key购买 nike

System.out.println("Thread state: " + threads[i].getState());
threads[i].notify();

产生以下输出:

Thread state: WAITING
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at MyPakc.An.run(An.java:49)
at java.lang.Thread.run(Thread.java:679)

这是怎么回事?为什么我无法通知 hibernate 线程?

编辑:threads[]类的代码:

package Part2;

import java.util.List;
import javax.swing.JPanel;



class BThread extends Thread{
private boolean completedThisIter = false;

@Override
public synchronized void run() {
while (true) {
completedThisIter = false;
doStuff()
System.out.println("Completed iter");
completedThisIter = true;
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public boolean getCompletedThisIter() {
return completedThisIter;
}
}

编辑:这是调用此函数的代码

public synchronized void run(){
// OTHER STUFF
for (int iter = 0; iter < 1212; ++iter){
System.out.println("Iter " + iter);
lastAssignedBallIndex = -1;
for (int i = 0; i < numThreads; i++) {
//System.out.println("Num " + numThreads + " " + i);
//ballThreads[i] = new BallThread(ballList.subList(lastAssignedBallIndex+1,lastAssignedBallIndex+numBallsPerThread),
// ballPanel);
//lastAssignedBallIndex += numBallsPerThread;
System.out.println("State " + ballThreads[i].getState());
if (ballThreads[i].getState() == Thread.State.NEW) {
ballThreads[i].start();
} else { //if (ballThreads[i].getState() == Thread.State.BLOCKED) {
System.out.println("Thread state: " + ballThreads[i].getState());
ballThreads[i].notify();
}
}
//try{
for (int i = 0; i < numThreads; i++) {
while (!ballThreads[i].getCompletedThisIter()) {
System.out.println("iter:" + iter + " ball:" + i + " " + ballThreads[i].getCompletedThisIter());
//wait(); // TODO elliminate polling here
}
}
System.out.println("Joined");
//}
// catch(InterruptedException ie){ie.printStackTrace();}


ballPanel.repaint();
notifyAll();
try{
Thread.sleep(2);
}
catch (InterruptedException ie){}
}
}

最佳答案

您正在打印 ballThreads[i] 的状态,然后通知 threads[i]。不确定这是否是预期的行为,但当您不拥有对象的监视器时,不允许您通知线程。您确定要在 threads[i] 对象的 synchronized() block 内调用此函数吗?

<小时/>

编辑:

Yes, the method that this code is taken out of is synchronized

编辑问题后,同步位于方法上,而不是对象的监视器上,您需要将代码放在如下所示的 block 中:

synchronized(threads[i]) {
// some stuff
threads[i].notify();
}

这里重要的一点(与方法声明中的 synchronized 关键字相反)是在对象上同步,然后在该 block 内调用 notify()对象上。示例:

public void run()
{
synchronized(myObject) {
// do some stuff
myObject.notify();
}
}

public void run()
{
synchronized(thread1) {
// do some stuff
thread1.notify();
}
}

public void run()
{
synchronized(syncObject) {
// do some stuff
syncObject.notify();
}
}

看到图案了吗?更多信息请点击:http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html

关于Java - 无法通知等待线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11589540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com