I am using a while loop to know when to stop the application and I'm using an executor service for threads:
ExecutorService executorService = Executors.newFixedThreadPool(8);
String[] error = {""};
while(!(error[0].equals("Nothing found"))) {
executorService.execute(new Runnable() {
@Override
public void run() {
addProductsToSet();
//rest of the code
synchronized (error[0]) {
error[0] = // code that might return "Nothing found" to stop the while loop
}
//rest of the code
} //end of run method
});
}
executorService.shutdown();
while(!executorService.isTerminated()) {
}
The problem that I have is that error[0] has the value "Nothing found" but the code just doesn't stop. It goes and goes on forever. If I'm not using threads, the while loop stops. I also tried executorService.shutdownNow() but doesn't work.
Any suggestions?
errorvariable volatile?while (!executorService.isTerminated())withexecutorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS), like the documentation for ExecutorService.shutdown() recommends.