gpt4 book ai didi

java - 等待线程永远不会醒来

转载 作者:行者123 更新时间:2023-11-29 08:06:08 24 4
gpt4 key购买 nike

我有一个带有两个线程的 ThreadManager。一种用于与图形用户界面相关的请求,另一种用于与测量相关的请求。他们都在运行并检查他们的请求队列,如果有,他们正在处理请求。可以随时添加请求,使用静态 ThreadManager.addGuiRequest(eGuiRequest)ThreadManager.addMeasRequest(eMeasRequest) 方法。现在这两个都需要初始化,这是通过向相应队列添加 INIT 请求来完成的。但是测量的初始化取决于gui已经初始化的事实。我尝试使用 wait()/notify() 解决此问题,但无法正常工作。

这是一个 SSCCE。在启动时,两个队列都添加了 INIT 请求,然后启动。测量初始化检测到 gui 尚未初始化并执行 wait()。 gui 初始化(模拟 sleep 5 秒)。这一切都很好。

gui 初始化后,它尝试唤醒测量线程,但测量线程没有唤醒...我的wait()/notify() 代码基于this article .这里出了什么问题?

import java.util.LinkedList;
import java.util.NoSuchElementException;

public class ThreadManager {
public static void main(String[] args) {
new ThreadManager();
ThreadManager.addMeasRequest(eMeasRequest.OTHER_STUFF);
}

public enum eGuiRequest { INIT, OTHER_STUFF; }
public enum eMeasRequest { INIT, OTHER_STUFF; }

private static LinkedList<eGuiRequest> guiQueue = new LinkedList<eGuiRequest>();
private static LinkedList<eMeasRequest> measQueue = new LinkedList<eMeasRequest>();
private static Thread guiThread, measThread;
protected boolean initialized = false;

public ThreadManager() {
final int waitMs = 200;
guiThread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
if (guiQueue.isEmpty()) sleepMs(waitMs);
else {
eGuiRequest req = guiQueue.getFirst();
processGuiRequest(req);
guiQueue.removeFirst();
}
} catch (NoSuchElementException e) {}
}
}

private void processGuiRequest(eGuiRequest req) {
System.out.println("T: " + "Processing Gui request: " + req);
switch (req) {
case INIT:
// do some initializiation here - replaced by a wait:
sleepMs(5000);
System.out.println("I: " + "guiThread finished, waking up measThread");
synchronized (measThread) {
initialized = true;
measThread.notify();
}
break;
case OTHER_STUFF:
// do other stuff
break;
}
}
});
measThread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
if (measQueue.isEmpty()) sleepMs(waitMs);
else {
eMeasRequest req = measQueue.getFirst();
processMeasurementRequest(req);
measQueue.removeFirst();
}
} catch (NoSuchElementException e) {}
}
}

private void processMeasurementRequest(eMeasRequest req) {
if (req == eMeasRequest.INIT) { // if init, wait until GUI is initialized
synchronized (this) {
while (!initialized) {
System.out.println("I: " + "measThread waits for guiThread to finish initializiation");
try {
wait();
} catch (Exception e) {}
System.out.println("I: " + "measThread awakes");
}
}
}
System.out.println("T: " + "Processing Measurement request: " + req);
// process request here:
sleepMs(5000);
}
});

addGuiRequest(eGuiRequest.INIT);
addMeasRequest(eMeasRequest.INIT);

guiThread.start();
measThread.start();
}

public static void sleepMs(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException ee) {}
}

public static void addGuiRequest(eGuiRequest req) {
guiQueue.add(req);
}

public static void addMeasRequest(eMeasRequest req) {
measQueue.add(req);
}
}

最佳答案

GUI线程在measThread(Thread类型)上调用notify()processMeasurementRequest()方法调用wait () on this,这是measThread使用的Runnable实例。

我建议使用一个由两个线程共享的特定对象来等待和通知:

private static final Object GUI_INITIALIZATION_MONITOR = new Object();

此外,我不会使用 LinkedList 并在请求之间 hibernate 任意时间,而是使用 BlockingQueue:这将允许消费线程在有请求时立即处理请求,并避免从 hibernate 状态不必要的唤醒.

此外,您可以使用初始化为 1 的 CountDownLatch,而不是低级等待/通知。GUI 线程将在初始化时 countDown() 锁存器,而测量线程将 await() 闩锁,直到 GUI 线程调用了 countDown()。这会将复杂的同步和通知内容委托(delegate)给更高级别、经过良好测试的对象。

关于java - 等待线程永远不会醒来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11116247/

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