Próba przerwania działającego wątku, w tym przykładzie, t1, który jest wykonywany przez wątek w puli wątków.Przerwanie wątku spania
t2 to ten, który wysyła przerwanie.
Nie mogę zatrzymać działającego t1, t1 nie otrzymuje wyjątku InterruptedException.
Czego mi brakuje?
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();
Dzięki BegemoT. To ma sens. – portoalet
begemot ma to prawo –