这样说对吗 - 即使在 DEADLINE 时间之后,java gRPC 服务器线程仍将运行。但是,gRPC 服务器只会停止/阻止该线程在超过 DEADLINE 时间后进行任何后续 gRPC 调用吗?
如果上面的陈述是正确的,那么有没有办法停止/阻止线程进行任何 Redis/DB 调用以及 DEADLINE 时间已经超过?还是一旦过了DEADLINE时间,就立刻打断线程?
最佳答案
Is it right to say that - java gRPC server thread will still run even after the DEADLINE time.
正确。 Java 不提供任何真正的替代品。
But, gRPC server will stop/block that thread only from making any subsequent gRPC calls since the DEADLINE time has crossed?
主要是。传出 gRPC 调用遵守 io.grpc.Context
,这意味着截止日期和取消会被传播(除非您未能将 Context 传播到另一个线程或使用 Context.fork()
) .
If the above is a correct statement, then is there a way to stop / block the thread making any Redis / DB calls as well for which DEADLINE time has crossed ? Or once the DEADLINE time is crossed, interrupt the thread immedietly?
您可以通过 Context.addListener()
监听 Context 取消。 gRPC 服务器将在截止日期到期时取消上下文,如果客户端取消 RPC。此通知是取消传出 RPC 的方式。
我会注意到线程中断有点涉及执行不竞争。如果您想要中断并且还没有 Future
,我建议将您的工作包装在 FutureTask
中(并简单地调用 FutureTask.run()
在当前线程上)以获得其非活泼的 cancel(true)
实现。
final FutureTask<Void> future = new FutureTask<Void>(work, null);
Context current = Context.current();
CancellationListener listener = new CancellationListener() {
@Override public void cancelled(Context context) {
future.cancel(true);
}
};
current.addListener(listener);
future.run();
current.removeListener(listener);
关于java - gRPC 服务器能否立即回收 DEADLINE_EXCEEDED 线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47506772/