I have a REST controller. It creates two threads, one is a scheduler that is searching data of a database for whether is exited by user and another one is a executor and returns 200 success code to client.
If main thread is checking another two thread, it works very well.
public boolean foo() {
//flag whether is exited by User
Boolean exited = false;
//create a scheduler
ScheduledExecutorService schedulService = Executors.newScheduledThreadPool(1);
//It is worked each 20 second
schedulService.scheduleWithFixedDelay(new FooSchdlue(exited), 0, 2000,TimeUnit.MILLISECONDS);
//create a executor
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<?> futrue = executor.submit(new FooExecutor());
// I want this Because this is not stopped
// Therefore I cant't return true until process is done
while (!exited && !futrue.isDone()) {
}
//All thread is exited
schedulService.shutdownNow();
futrue.cancel(true);
return true;
}
However, it can't return true until another two threads are done.
// I want this Because this is not stopped
// Therefore I cant't return true until process is done
//while (!exited && !futrue.isDone()) {
//}
//All thread is exited
//schedulService.shutdownNow();
//futrue.cancel(true);
I want to shutdownNow or cancel another thread without main thread.
class FooSchdlue implements Runnable{
Boolean exited = false;
public FooSchdlue(Boolean exited) {
this.exited = exited;
}
@Override
public void run() {
// Database check
if(foo.getExitFlag() == true) {
exited = true;
***exit Another sub thread***
}
}
}
I saw this, how to stop main thread while another thread still running, but I think it is opposite of my situation.