In Java there is no reliable way to "kill" a thread in process. However, when cancel is called on a Task, it is important that the Task stop processing. A "run-away" Task might continue processing and updating the message, text, and progress properties even after the Task has been cancelled! In Java, cancelling a Task is a cooperative endeavor. The user of the Task will request that it be cancelled, and the author of the Task must check whether is has been cancelled within the body of the call method. There are two ways this can be done. First, the Task author may check the isCancelled method, inherited from FutureTask, to see whether the Task has been cancelled. Second, if the Task implementation makes use of any blocking calls (such as NIO InterruptibleChannels or Thread.sleep) and the task is cancelled while in such a blocking call, an InterruptedException is thrown. Task implementations which have blocking calls should recognize that an interrupted thread may be the signal for a cancelled task and should double check the isCancelled method to ensure that the InterruptedException was thrown due to the cancellation of the Task.
Campbell Ritchie wrote:I don't know whether things are different for JavaFX, but it would be dangerous to create multiple threads in Swing. Cai Horstmann says it has never been possible to make a thread‑safe GUI framework that executes faster than a half‑dead snail. You have one thread and one only that accesses the display. If you are doing something time‑consuming, which searching a hard drive would be, you can start a worker thread which does all the searching completely independently from the display.
I don't know whether things are different for JavaFX, but it would be dangerous to create multiple threads in Swing. Cai Horstmann says it has never been possible to make a thread‑safe GUI framework that executes faster than a half‑dead snail. You have one thread and one only that accesses the display. If you are doing something time‑consuming, which searching a hard drive would be, you can start a worker thread which does all the searching completely independently from the display.
That “own thread” sounds like a worker thread, so the situation sounds just the same as in Swing.John Damien Smith wrote:. . . That doesn't mean that you can't write multi-threaded JavaFX programs using JavaFX. You just need to make sure that anything which effects the active scene graph (properties of nodes which are currently in a displayed scene), is only modified on the JavaFX application thread. But anything which is not directly effecting the active scene graph (including modifications to nodes that not part of the active scene graph) can be written in its own thread. . . .
What a good idea.John Damien Smith wrote:. . . You don't need to use all of the advanced libraries in the java.util.concurrent package. You can stick to very simple constructs . . .
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
|