Java Programming - Threads - Discussion


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


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

Nicole said: (Jan 15, 2018)  
To figure out why the second "Inner Thread", I recommend all of you have a try at changing the sentence "new MyThread (new myRunnable()).Start() ;" into "new Thread (new myRunnable()).Start() ;", and look at the output. Then you would understand why and understand the description!

David said: (Mar 18, 2018)  
It will give the compilation error. Because you can not call super with an argument.

MyThread (Runnable r) {super (r);}.

Priyo said: (Jun 21, 2018)  
MyThread constone. barException in thread "main" java.lang.IllegalThreadStateException.
At java.lang.Thread.start(Unknown Source).
At io.priyo.MutiThreding.Mythread1.main(

Rksh25 said: (Nov 20, 2019)  
Actually it is tricky but Let Me try to share my opinion:-
new MyThread(new MyRunnable()).start();

A new thread object will be created(No reference variable). MyThread is also a Thread since it extends Thread. It has its own run method within the class. You are calling start() method on it. It will run its own run method. We are NOT writing:

new Thread(new MyRunnable()).start();. If we write so, Inside my Runnable will be called.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.