Why in the code below "public void run() throws InterruptedException" creates a compilation error but "public void run() throws RuntimeException" does not?
The compilation error raised by InterruptedException is"Exception InterruptedException is not compatible with throws clause in Runnable.run()"
Is it because RuntimeException is unchecked exception therefore does not change the run() signature?
public class MyThread implements Runnable{
String name;
public MyThread(String name){
this.name = name;
}
@Override
public void run() throws RuntimeException{
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(Math.round(100*Math.random()));
System.out.println(i+" "+name);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Thread thread1 = new Thread (new MyThread("Jay"));
Thread thread2 = new Thread (new MyThread("John"));
thread1.start();
thread2.start();
}
}