杀死来自完成服务的线程?

huangapple go评论87阅读模式
英文:

Killing threads from completion service?

问题

问题

我正在使用完成服务(completion service)并创建子线程来执行一些 ETL 操作。当我在我的 IDE 中进行调试,然后停止所有进程时,我注意到仍然有许多僵尸线程在占用我的 CPU。这是因为我没有正确地终止子线程。

最简示例


  Future<Boolean> future = completionService.submit(conversionProcessor);
  boolean isCompleted = false;
  while (!isCompleted && !closed.get()) {
    try {
      isCompleted = future.get(CONSUMER_HEARTBEAT_INTERVAL,
          TimeUnit.SECONDS); // 等待直到心跳间隔超过
      if (isCompleted) {
        // 在这里执行一些操作
        future.cancel(true);
        break;
      }
    } catch (TimeoutException e) {
      // 用于保持消费者在集群中活动
      consumer.poll(Duration.ofSeconds(CONSUMER_HEARTBEAT_INTERVAL)); // 执行心跳操作
    } catch (CancellationException e) {
      future.cancel(true);
      break;
    } catch (InterruptedException e) {
      future.cancel(true);
      break;
    } catch (WakeupException we) {
      future.cancel(true);
      break;
    } catch (Exception e) {
      future.cancel(true);
      break;
    }

想法

基本上,我将我的 Callable<Boolean> 提交给了完成服务。

    ExecutorCompletionService<Boolean> completionService = new ExecutorCompletionService<>(
        Executors.newSingleThreadExecutor());

如果我停止调试器,这个线程很可能仍在运行。我刚刚添加了 future.cancel(true) 这一部分,似乎已经停止了来自子线程的连续上传文件,但我仍然在活动监视器中看到这些 Java 进程在运行。

我想知道我应该如何考虑这个问题?我想要 Callable<Boolean>,因为它告诉我底层的 ETL 操作是否已经完成(true/false)。

编辑:
实际上,future.cancel 似乎帮助了不少... 这是我应该使用的方法吗?

英文:

Problem

I am using a completion service and spawning child threads to perform some ETL.
As I debug in my IDE and then stop all processes, I notice I still have a bunch of zombie threads killing my CPU. This is due to the fact that I'm not terminating the child threads properly.

Minimum Example


  Future&lt;Boolean&gt; future = completionService.submit(conversionProcessor);
  boolean isCompleted = false;
  while (!isCompleted &amp;&amp; !closed.get()) {
    try {
      isCompleted = future.get(CONSUMER_HEARTBEAT_INTERVAL,
          TimeUnit.SECONDS); // Wait until heartbeat interval exceeds
      if (isCompleted) {
        // do some things here
        future.cancel(true);
        break;
      }
    } catch (TimeoutException e) {
      // Used to keep consumer alive in the cluster
      consumer.poll(Duration.ofSeconds(CONSUMER_HEARTBEAT_INTERVAL)); // does heart-beat
    } catch (CancellationException e) {
      future.cancel(true);
      break;
    } catch (InterruptedException e) {
      future.cancel(true);
      break;
    } catch (WakeupException we) {
      future.cancel(true);
      break;
    } catch (Exception e) {
      future.cancel(true);
      break;
    }

Thoughts

Essentially, I submit my Callable&lt;Boolean&gt; to my completion service.

    ExecutorCompletionService&lt;Boolean&gt; completionService = new ExecutorCompletionService&lt;&gt;(
        Executors.newSingleThreadExecutor());

If I stop the debugger, this thread is presumably still running. I just added this future.cancel(true) piece, which seems to have stopped continuously uploaded files from my child thread, but I still see these java processes running on my activity monitor.

I'm wondering how I should be thinking about this? I want the callable<boolean> as it tells me when the underlying ETL has completed or not (true/false)

edit:
future.cancel actually seems to be helping quite a bit.. Is this what I want to be using?

答案1

得分: 2

完成CompletionService后,您需要关闭底层的执行器,因此需要执行以下操作:

ExecutorService es = Executors.newSingleThreadExecutor();
ExecutorCompletionService<Boolean> completionService = new ExecutorCompletionService<>(es);

在结束时调用:

es.shutdown();
es.awaitTermination(1, TimeUnit.SECONDS);
英文:

Once you are done with your CompletionService you need to shutdown underlying executor so you need to do the following

ExecutorService es = Executors.newSingleThreadExecutor();
ExecutorCompletionService&lt;Boolean&gt; completionService = new ExecutorCompletionService&lt;&gt;(es);

And a the end call

es.shutdown();
es.awaitTermination(1, TimeUnit.SECONDS);

huangapple
  • 本文由 发表于 2020年9月12日 02:31:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63852500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定