enter image description here Les threads ne sont pas arrêtés même après avoir appelé la méthode Interruption from Executors shutdownNow.
La fonctionnalité d'appel de thread s'exécute dans une boucle while, qui recherche l'indicateur d'interruption. J'ai essayé d'envoyer l'indicateur Interruption au thread en cours d'exécution après un certain temps, mais il est toujours en cours d'exécution. Je veux forcer l'arrêt du thread. Quelqu'un peut-il dire ce comportement. Fixation l'exemple de code Java:Quelle est la manière élégante d'arrêter un thread en cours d'exécution à partir du service d'exécution
public class TestExecutor {
static volatile int j = 1;
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(5);
for (int i = 1; i <= 10; ++i) {
Future<Map<String, List<String>>> abc = pool.submit(new Callable<Map<String, List<String>>>() {
volatile boolean abortFlag = false;
@Override
public Map<String, List<String>> call() throws Exception {
while(!abortFlag){
long start = System.currentTimeMillis();
for(int k=0; k < 10e4 ;k++){
abortFlag = abort();
System.out.println(Thread.currentThread().getName() +" " +abortFlag);
}
System.out.println("counter val is:" +Thread.currentThread().getName() +" : " +j++);
long end = System.currentTimeMillis();
System.out.println("time for one execution : " +" " +Thread.currentThread().getName() +" :" +(end-start));
return null;
}
return null;
}
private boolean abort() {
if(Thread.currentThread().isInterrupted()){
return true;
}else{
return false;
}
}
});
}
pool.shutdown();
try {
if (pool.awaitTermination(3000, TimeUnit.MILLISECONDS)) {
System.out.println("task completed");
} else {
System.out.println("Forcing shutdown...");
pool.shutdownNow();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Closed");
}
} `
double possible de [? Comment arrêter l'exécution de Exécuteur ThreadPool en java] (https://stackoverflow.com/questions/1562079/ how-to-stop-the-execution-of-executor-threadpool-in-java) – alain
Fonctionne bien ici. Le programme s'arrête. –
@JB: J'ai posté l'image aussi où vous pouvez voir qu'elle est toujours en cours d'exécution après que le drapeau d'abandon est vrai. S'il vous plaît voir dans edit. –