I have thread application which process heavy task, i would like to trigger forcibly stop thread upon external flag information. I have tried following design,
public class HeavyTaskThread implements Runnable
{
private boolean forceStop;
public void run()
{
..
..
..
..
}
}
Another fact is i do not have control of logic flow implemented into method run(); which simply call some third-party program. I was trying with light inner thread class and calling method interrupt() on parent thread, but this does not work.
Please suggest any pattern….
Here is final analysis…for killing thread.
Using Thread to control the execution of Program B. But then stopping a process means via a thread is not allowed as the related methods are deprecated (stop/suspend etc.,)
Using ThreadGroup ( to have only one thread as its member) and calling ‘destroy’ on the group. But it again falls on the same track as every thread should be stopped before the destroy operation is attempeted.
Process/ProcessBuilder via Runtime seems to be the better way to obtain a process reference and call destroy(), waitFor() etc. as user @Tudor mentioned.
Here is exact outlined code i have tried and it fails to kill, commented line while(isValid()) represents my another java program invoke.