0

I have a method called runClient that is blocking, basically it never ends. My first instinct was to put it into a Thread, but I can't stop it because stop() is deprecated, and I cannot check for an interrupt if the method is always blocking.

Is there some way to run it async, and then be able later to stop/destroy the execution of the method?

Markusbug
  • 31
  • 6
  • You tagged completable future, which is indeed what you could use. Check CompletableFuture.runAsync – Matteo NNZ Nov 15 '22 at 20:50
  • @MatteoNNZ Nice, will give it a go – Markusbug Nov 15 '22 at 20:52
  • 1
    Have you already read: [How to properly stop the Thread in Java?](https://stackoverflow.com/questions/10961714/how-to-properly-stop-the-thread-in-java) ? – LuCio Nov 15 '22 at 20:53
  • @MatteoNNZ Do you know how I can stop the completable-future from executing? – Markusbug Nov 15 '22 at 20:54
  • @Markusbug: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html#cancel-boolean- . (This won't necessarily stop the work from running, through any mechanism other than interruption. It does not solve your real problem.) – Louis Wasserman Nov 15 '22 at 21:45
  • 1
    Isn’t this essentially the same question you posted earlier? https://stackoverflow.com/q/74450934/29470 – Tim Moore Nov 15 '22 at 21:59
  • 1
    Does this answer your question? [How do I kill a blocking thread? (Cannot check for interruption)](https://stackoverflow.com/questions/74450934/how-do-i-kill-a-blocking-thread-cannot-check-for-interruption) – michid Nov 16 '22 at 06:45
  • @Markusbug you have a .cancel() method on the instance of CompletableFuture that is returned by the runAsync – Matteo NNZ Nov 16 '22 at 09:30
  • 1
    @LouisWasserman it does not even stop the work via interruption, as stated explicitly. – Holger Nov 16 '22 at 16:16

0 Answers0