How can I make external methods interruptable?

后端 未结 8 910
南旧
南旧 2021-02-05 02:32

The Problem

I\'m running multiple invocations of some external method via an ExecutorService. I would like to be able to interrupt these methods, but unfortunately the

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-05 03:24

    Like mhaller said, the best option is to launch a new Process. Since your jobs are not that cooperative, you will never have guarantees on the Thread termination.

    A nice solution to your problem would be using a library that supports arbitrary pause/stop of ' lightweight threads' such as Akka instead of the executor service, although this may be a bit of an overkill.

    Although I've never used Akka and cannot confirm it works as you expect, the docs state there's a stop() method for stopping actors.

提交回复
热议问题