Interview Questions

Output question 16.

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

76. Output question 16.

public class MyClass implements Runnable{
@Override
public void run() {
System.out.println("1");
}

public static void main(String[] args) {
MyClass obj=new MyClass();
Thread thread1=new Thread(obj,"Thread-1");
thread1.start();
thread1.start();
}
}


Answer.
we cannot start Thread again, doing so will throw runtimeException java.lang.IllegalThreadStateException. The reason is once run() method is executed by Thread, it goes into dead state.
Let’s take an example-
Thinking of starting thread again and calling start() method on it (which internally is going to call run() method) for us is some what like asking dead man to wake up and run. As, after completing his life person goes to dead state.


/*OUTPUT
1
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
*/

(Continued on next question...)

Other Interview Questions