Sunday, November 24, 2013

Question - Multithreading - Thread Execution

Given:
 public class Threads2 implements Runnable {
public void run() {
System.out.println("run.");
throw new RuntimeException("Problem");
}
public static void main(String[] args) {
Thread t = new Thread(new Threads2());
t.start();
System.out.println("End of method.");
}
}

Which of the following two can be results of above question

A. java.lang.RuntimeException: Problem
B. run. java.lang.RuntimeException: Problem
C. End of method. java.lang.RuntimeException: Problem
D. End of method. run. java.lang.RuntimeException: Problem
E. run. java.lang.RuntimeException: Problem End of method.

Execution Result:
Execution of thread is unpredictable and is dependent on JVM
so sometimes main thread will run and it will print End of Method and then other thread will print run and Exception
and other time main thread will run later on to execution of other thread.

So answer will be :
D,E
D. End of method. run. java.lang.RuntimeException: Problem
E. run. java.lang.RuntimeException: Problem End of method.

No comments:

Post a Comment