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.

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

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.

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

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!

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.

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

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.

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(Mythread1.java:10).

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.

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.


Post your comments here:

Your comments will be displayed after verification.