Terminate thread properly in a plugin

Dear community,

I’m making a ImageJ plugin for a user to enable online tracking of moving cells in a time-lapse image stack. For that I wrote a function takes an initial ROI input to detect a cell, and then tracking that cell in the next frames until it cannot be detected anymore.

However many times in the data, cell cross each other or getting too dense, so a simple auto tracking would fail. Instead of designing a sophisticated tracking algorithm, currently with the user we agreed on just to visually monitor the process and stop the auto-tracking when it makes an error. This has been done easily with plugin frame and a customised button. The button starts a new thread that runs the auto-tracking function, and if it’s already running, the button click stops it.

Initially I was using Thread.stop() to stop the running tracking thread, but later I found this method has been deprecated for a long time (https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#stop--). And then I followed up on how to terminate a thread properly in Java, and found people suggesting either use a volatile boolean flag for thread to check inside run() function, or use thread.interrupt(). But when I checked the source code of ThreadKiller in Fiji, it seems still using thread.stop() to terminate certain thread. And ThreadKiller is my choice of killing infinite loop threads and normally works well.

Therefore, I’m wondering if there’s now a standard in the community to handle thread start and stop properly in Java? Or if I’m over-thinking? Then I would be glad to simply use Thread.stop upon confirmation.

Thanks for your input.

Bests,
Ziqiang Huang