Java Programming - Threads - Discussion

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(); 
    } 
}

[A]. Prints "Inside Thread Inside Thread"
[B]. Prints "Inside Thread Inside Runnable"
[C]. Does not compile
[D]. Throws exception at runtime

Answer: Option A

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.


Saurabh Vyas said: (Jan 6, 2011)  
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: (Jan 6, 2011)  
i got the solution it was a good question. the answer is correct

Charu said: (Sep 4, 2011)  
Could anyone suggest why the answer is A for this. As not satsifed by the answer.

Chintha said: (Feb 16, 2012)  
If MyThread class doesn't override the run method, the output will b A.

Sriram said: (Aug 20, 2012)  
See the run() method in MyRunnable is never invoked.

Manish Srivastava said: (Sep 28, 2012)  
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: (Mar 4, 2015)  
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: (Jan 26, 2016)  
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: (Mar 29, 2016)  
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: (Jun 6, 2016)  
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.

Prashantp224 said: (Mar 18, 2017)  
New MyThread().start();

Reason 1-
Here thread1 is running by the object of Mythread class .( We can say object of default constructor of Mythread )

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

Reason 2-
Here thread2 is started by different object which is new MyThread(new MyRunnable());
(This thread2 is created by parameterize constructor of MyThread class .which is taking object of MyRunnable as input .)

So By Reason1 and Reason2 we can say that there are two different threads of Mythread class .

Eg : thread1.start();
thread2.start();
which is legal .

These both threads are object of Mythread which is containing common ru(); method for both of them .
That's why both the time.

Inside Mythread will print both the time .

Peter said: (Sep 25, 2017)  
When these lines are executed:

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

You are calling the MyThread class 2 times.
To call the MyRunnable class, you would execute:
new MyRunnable().run();

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.