JavaFX stop threads


Question

I write a program which needs to receive informations from a server. I use asynchronous threads to accomplish that. But when the user clicks on another item a new thread gets launched. If the first one has not yet finished for example because of a network issue, the second thread ends and sets the UI information. Then the first one ends and overwrites it again. So how can I stop the first thread when the user clicks on another item?

I got that:

Thread asyncThread = new Thread(task);
asyncThread.start();

But from what I can see in the Thread object, there is no way to stop the thread (only deprecated .stop()), but I don't want to use deprecated stuff for new programs.

1
1
6/22/2013 9:36:56 PM

In JavaFX it's recommended to use Service, a service provides method like, stop(), cancel(), restart()... Look my answer to this JavaFX GUI stuck to show how use Service.

0
5/23/2017 10:32:06 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon