My task is to make the multiple threads execute the never-ending task/ make a never-ending threads which will run the same 'void' method again and again, until the trigger will finish it.
Here is 1st attempt Execute a continious task via ThreadPoolExecutor
Now trying with Threads, but still can't get the needed result. Here it is:
boolean IsRunning = true;
Integer noThreads = 5;
ThreadGroup tg = new ThreadGroup("A");
Thread[] listThreads = new Thread[noThreads];
tg.enumerate(listThreads);
@RequestMapping(value = "/start_new", method = RequestMethod.POST)
public Callable<String> StartNewTask(@RequestBody LaunchSend sendobj) throws IOException, InterruptedException {
Runnable runnable = () -> {
while(IsRunning) {
MyVoid();
}
};
for (int i = 0; i < noThreads; i++) {
listThreads[i] = new Thread(runnable);
listThreads[i].start();
}
return () -> "Callable result";
}
@Async
void MyVoid(){
Globals.getInstance().increment();
System.out.println(Thread.currentThread().getName()+" iteration # "+ Globals.getInstance().Iterator);
}
The method 'MyVoid' is in while(true) {} cycle, so it's supposed to be executed continually, but it doesn't. Each Thread execute it only once.