Tentativo di interrompere un thread in esecuzione, in questo esempio, t1, che viene eseguito da un thread in un pool di thread.Interruzione di un thread in sospensione
t2 è quello che invia l'interrupt.
Impossibile interrompere l'esecuzione t1, t1 non ottiene InterruptedException.
Cosa mi manca?
Executor exec1 = Executors.newFixedThreadPool(1);
// task to be interrupted
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
System.out.println("starting uninterruptible task 1");
Thread.sleep(4000);
System.out.println("stopping uninterruptible task 1");
} catch (InterruptedException e) {
assertFalse("This line should never be reached.", true);
e.printStackTrace();
}
}
};
final Thread t1 = new Thread(runnable);
// task to send interrupt
Runnable runnable2 = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
t1.interrupt();
System.out.println("task 2 - Trying to stop task 1");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread t2 = new Thread(runnable2);
exec1.execute(t1);
t2.start();
t2.join();
Grazie BegemoT. Questo ha senso. – portoalet
begemot ha ragione –