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(); 
    } 
}
Prints "Inside Thread Inside Thread"
Prints "Inside Thread Inside Runnable"
Does not compile
Throws exception at runtime
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 1 of 2.

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.

Saurabh vyas said:   1 decade ago
i got the solution it was a good question. the answer is correct

Charu said:   1 decade ago
Could anyone suggest why the answer is A for this. As not satsifed by the answer.

Chintha said:   1 decade ago
If MyThread class doesn't override the run method, the output will b A.

Sriram said:   1 decade ago
See the run() method in MyRunnable is never invoked.

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.

Praveen said:   9 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.

Syed said:   8 years ago
new MyThread().start();

It create the object and calling the start so run method is executing inside the mythread class.

new MyThread(new MyRunnable()).start();

In this start method calling from mythread object new MyThread(new MyRunnable()).start(); NOT a new MyRunnable().start();

Confused said:   8 years ago
What with Garbage Collector behavior?

We create an instance of MyThread, then we run this thread, and immediately after that, we do not have any connector with this executing thread.

In common GC will remove the object, in that case (I conclude) GC allows finish thread.

Rahul said:   8 years ago
The correct option is c.

Because Runnable is abstract it can not be instantiated so this new MyThread(new Runnable()).start(); will not compile and throws a error.


Post your comments here:

Your comments will be displayed after verification.