java - gRPC 服务器能否立即回收 DEADLINE_EXCEEDED 线程?

标签 java multithreading threadpool grpc grpc-java

这样说对吗 - 即使在 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() 监听 Co​​ntext 取消。 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/

相关文章:

java - "seda + concurrentConsumers"和 "direct + threads"有什么区别

C++ POCO - 如何在不使用 run() 方法的情况下在线程池上启动线程?

python - python 中的线程-

java - 每次应用程序重新启动时 Spring Integration 都会加载文件

c# - WCF服务停止处理调用15秒钟

Java并行矩阵乘法

Android 计算应用程序/进程的线程数

java - 禁用 Struts2 日志

java - android.view.InflateException : Binary XML file line #11: Binary XML file line #39: Error inflating class support. v4.view.ViewPager

java - 所有线程方法(如 getName/setName)都是线程安全的吗?