Olá,
Estava estudando Threads para certificação e me deparei com esse código:
class MyThread implements Runnable {
String name; // name of thread
Thread t;
MyThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start();
}
public void run() {
try {
for (int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
}
public class MainClass {
public static void main(String args[]) {
MyThread ob1 = new MyThread("One");
MyThread ob2 = new MyThread("Two");
MyThread ob3 = new MyThread("Three");
System.out.println("Thread One is alive: " + ob1.t.isAlive());
System.out.println("Thread Two is alive: " + ob2.t.isAlive());
System.out.println("Thread Three is alive: " + ob3.t.isAlive());
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: " + ob1.t.isAlive());
System.out.println("Thread Two is alive: " + ob2.t.isAlive());
System.out.println("Thread Three is alive: " + ob3.t.isAlive());
System.out.println("Main thread exiting.");
}
}
Gostaria de entender melhor quando que pode ocorrer uma InterruptedException. Nesse código sou obrigado a tratá-la em duas partes do código, mas ela não é realmente lançada. Poderiam me explicar melhor e com detalhes a respeito da InterruptedException ?
Achei muito vaga a explicação da documentação oficial:
"Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity."
Se ela pode ser lançada quando a thread está em sleep, waiting ou ocupada, quando a thread está em atividade ou não, então essa exceção pode ser lançada a qualquer momento praticamente?
Obrigado.