Java Programming - Threads - Discussion

Discussion Forum : Threads - Finding the output (Q.No. 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

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.

17 comments Page 1 of 2.

Afzal khan said:   1 year ago
The right Answer is ->Prints "Inside Thread Inside Runnable" not the ->Prints "Inside Thread Inside Thread".

Please correct it.

Rksh25 said:   4 years ago
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.

Priyo said:   6 years ago
MyThread constone. barException in thread "main" java.lang.IllegalThreadStateException.
At java.lang.Thread.start(Unknown Source).
At io.priyo.MutiThreding.Mythread1.main(

David said:   6 years ago
It will give the compilation error. Because you can not call super with an argument.

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

Nicole said:   6 years ago
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!

Peter said:   7 years ago
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();

Prashantp224 said:   7 years ago
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 .

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.

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.

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

Post your comments here:

Your comments will be displayed after verification.