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();
}
}
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.
Afzal khan said:
2 years ago
The right Answer is ->Prints "Inside Thread Inside Runnable" not the ->Prints "Inside Thread Inside Thread".
Please correct it.
Please correct it.
(1)
Confused said:
9 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.
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.
Rksh25 said:
5 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.
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(Mythread1.java:10).
At java.lang.Thread.start(Unknown Source).
At io.priyo.MutiThreding.Mythread1.main(Mythread1.java:10).
David said:
7 years ago
It will give the compilation error. Because you can not call super with an argument.
MyThread (Runnable r) {super (r);}.
MyThread (Runnable r) {super (r);}.
Nicole said:
7 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();
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:
8 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 .
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 .
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.
Because Runnable is abstract it can not be instantiated so this new MyThread(new Runnable()).start(); will not compile and throws a error.
Saurabh vyas said:
1 decade ago
i got the solution it was a good question. the answer is correct
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers