大家好:这个问题涉及到线程、客户端、Netty、Ning。
上下文
Ning 库是一个异步 HTTP 请求工具,它允许我们构建请求、对它们进行排队并异步处理它们。它依赖于一些库,例如下面粘贴的 JBoss/Netty 代码。
问题
我最近遇到了一个线程关闭异常,该异常是由 JBoss/Netty ExecutorUtil
类中的这段代码引发的。
这个类本质上是一个用于结束 Netty 线程的实用程序。
此方法导致我正在使用的 Web 客户端(由 Ning 提供支持)中出现错误,因为我试图关闭该客户端响应的处理程序内的 HttpClient。
问题
这段代码试图避免的死锁有什么意义?更详细的类(class)可以查看 this code url .
// Check dead lock.
final Executor currentParent = DeadLockProofWorker.PARENT.get();
if (currentParent != null) {
for (Executor e: executorsCopy) {
if (e == currentParent) {
throw new IllegalStateException(
"An Executor cannot be shut down from the thread " +
"acquired from itself. Please make sure you are " +
"not calling releaseExternalResources() from an " +
"I/O worker thread.");
}
}
}
最佳答案
在 netty 中,不允许您从 IO 线程内关闭执行器(因为异常已经告诉您了;)),这是因为您将面临死锁的风险。如果您确实想从处理程序中关闭执行器,则需要在新线程中执行此操作。例如:
public void messageReceived(...) {
// in IO-Thread
new Thread(new Runnable() {
public void run() {
//....shutdown here
}
}).start();
}
关于java - 执行器无法从自身获取的线程中关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10114838/