Java Programming - Threads - Discussion
Discussion Forum : Threads - Finding the output (Q.No. 3)
3.
What will be the output of the program?
class MyThread extends Thread
{
MyThread() {}
MyThread(Runnable r) {super(r); }
public void run()
{
System.out.print("Inside Thread ");
}
}
class MyRunnable implements Runnable
{
public void run()
{
System.out.print(" Inside Runnable");
}
}
class Test
{
public static void main(String[] args)
{
new MyThread().start();
new MyThread(new MyRunnable()).start();
}
}
Answer: Option
Explanation:
If a Runnable object is passed to the Thread constructor, then the run method of the Thread class will invoke the run method of the Runnable object.
In this case, however, the run method in the Thread class is overridden by the run method in MyThread class. Therefore the run() method in MyRunnable is never invoked.
Both times, the run() method in MyThread is invoked instead.
Discussion:
17 comments Page 2 of 2.
Praveen said:
8 years ago
I want know. The parameter pass to the mythread then it will run and print out ok but how another time thread is started. Actually I want to know how thread start twisty.
Manish Srivastava said:
1 decade ago
In mytread class runnable class pass as a parameter sp in this reason MyThread constructor contain runnable parameter type variable. Control goes to mythread class and print run method of mythread. In this way correct answer is A.
Sriram said:
1 decade ago
See the run() method in MyRunnable is never invoked.
Chintha said:
1 decade ago
If MyThread class doesn't override the run method, the output will b A.
Charu said:
1 decade ago
Could anyone suggest why the answer is A for this. As not satsifed by the answer.
Saurabh vyas said:
1 decade ago
i got the solution it was a good question. the answer is correct
Saurabh vyas said:
1 decade ago
Here in this question again the start method is invoked two times.
I am confused won't here be also IllegalThreadStateException will be thrown because we can register only one thread at a time.
I am confused won't here be also IllegalThreadStateException will be thrown because we can register only one thread at a time.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers