Discussion :: Threads
-
What will be the output of the program?
class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); t.start(); System.out.print("one. "); t.start(); System.out.print("two. "); } public void run() { System.out.print("Thread "); } }
A.
Compilation fails |
B.
An exception occurs at runtime. |
C.
It prints "Thread one. Thread two." |
D.
The output cannot be determined. |
Answer : Option B
Explanation :
When the start() method is attempted a second time on a single Thread object, the method will throw an IllegalThreadStateException (you will not need to know this exception name for the exam). Even if the thread has finished running, it is still illegal to call start() again.
Be The First To Comment