This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When both Thread and Runnable have public void run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "When both Thread and Runnable have public void run" Watch "When both Thread and Runnable have public void run" New topic
Author

When both Thread and Runnable have public void run

M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
I was curious on what happens if both the runnable and thread had overridden the run method.



Guess what it prints:

executing thread run.
Kalyani Marathe
Ranch Hand

Joined: Apr 06, 2005
Posts: 72
Hi Rama,

I guessed it. I wasn�t sure.
I want to clarify:
1. When anonymous class is created it will print "executing thread run" and later on will call constructor of thread.
2. When you say t.start(), it will call its own start and run. It won�t see for run defined here. And run() just satisfies the interface runnable.

Am I interpreting right?

Thanks in advance.
nagaraj reddy
Ranch Hand

Joined: Feb 21, 2005
Posts: 43
Kalyani

the anonymous class is sub class of startInThreadAndRunnable calss, then the reference variable r(Thread r) holds the object of that subclass.

when you made a call to r.start(),it will call to the StartInThreadAndRunnable class start() method and it inturns call the overided run() method in the subclass.

thats why answer is.
This is procedure i think so.
If it is not please explain anybody

thanks in advance
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"Kalyani", please take a little time to read our JavaRanch Naming Policy and change your displayed name to conform with it.
In your case we need two names in the format <first name> <family name> (and in that order).
Thanks
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
The short (and only needed) answer is that Thread implements Runnable and Runnable defines run() therefore Threads needs to implement run().


42
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Jeroen]: The short (and only needed) answer is that Thread implements Runnable and Runnable defines run() therefore Threads needs to implement run().

But the Thread class already does implement run(). If run() had not been overridden, that's the version of run() that would have ran. Since run() was overridden, the default behavior of Thread's run() method did not get called, and the overriding method (in the anonymous class) did get called[/i]. So consider: what would have happened if the anonymous class did not override the run() method? And then, since run() actually is overridden, how did that change the result?


"I'm not back." - Bill Harding, Twister
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
If a Thread is created to use a Runnable the run() method from that Runnable will be used instead of the run() method of the Thread itself (or rather the run() method of the Thread() will call the run() method of a passed Runnable().
This becomes readily apparent when investigating the code for Thread which either calls a run() method on a supplied Runnable or terminates immediately.

If run() in Thread is overridden to provide different behaviour and the superclass run() isn't called the run() from the Runnable may well be ignored completely (depending on the specific implementation of run() in the Thread subclass.

Given the scenario given, it should therefore come as no surprise that the inclusion of a Runnable implementation in the Thread constructor will have had no influence on the outcome of the Thread's start method.
That is of course, unless start() itself is overridden to ignore the run() method in the class itself and use that of the subclass (which would require implementing native code to replace the default multithreading code, probably outside the scope of the OPs question).
M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
Here is the code other way around where I override runnable.


The output is :

executing thread run




Which means that the thread object first looks at its own run method and if doesn't find out, will look at the runnable object's.
M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
This program will make it even more clear:

I have overriden the thread's run and am calling super.run() inside.



prints:

executing runnable run
executing thread run
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Originally posted by M Rama:

Which means that the thread object first looks at its own run method and if doesn't find out, will look at the runnable object's.


The thread object *only* calls its own run() method. However, if you don't override it, the default run() method calls the attached runnable run() method.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
lauren bai
Greenhorn

Joined: Apr 08, 2005
Posts: 18
Inspried by M Rama, I also write a class. I don't know if it's illegal.

public class StartInThreadAndRunable extends Thread implements Runnable {
StartInThreadAndRunable(){
}

StartInThreadAndRunable(Runnable r) {
super(r);
}

public void run() {
System.out.println("Thread or Runnable?" + this);
}

public static void main(String[] args) {
Runnable r = new StartInThreadAndRunable();
Thread t2 = new StartInThreadAndRunable(r);
t2.start();
}
}
Output: Thread or Runnable? Thread[Thread-1,5,main]


I want to know what's Thread[Thread-1,5,main] mean?


SCJP (In Progress)
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Lauren,

I want to know what's Thread[Thread-1,5,main] mean?

Thread-1, 5 and main represent the thread's name, priority and thread group respectively.

For more info, check out the Thread API.

Joyce
[ April 14, 2005: Message edited by: Joyce Lee ]
lauren bai
Greenhorn

Joined: Apr 08, 2005
Posts: 18
Thread-1, 5 and main represent the thread's name, priority and thread group respectively.


Thank you, Joyce. I just want to know the run method in my class overrides the run method in Threads or Runnable class?
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Lauren: I just want to know the run method in my class overrides the run method in Threads or Runnable class?

The run() method in StartInThreadAndRunable overrides the one in the Thread class.

By studying the source code of the Thread class, you'll have a better understanding of how it works. The source code is available in the src.zip under your JDK-xxx directory.

Below is a small portion of Thread's source code:


A Runnable object that passed into a Thread constructor will be stored in the target variable. The Thread's run() will in turn call the target.run(). So if run() in Thread class is overridden, target.run() will not be called.

Here is a simple example:



The output is "ThreadTest: run" because run() is overridden in the ThreadTest.

If you comment or remove run() in ThreadTest, the run() in Thread class will be called which in turn calls the target.run(). So the output will be "RunnableTest: run".

Joyce
[ April 15, 2005: Message edited by: Joyce Lee ]
lauren bai
Greenhorn

Joined: Apr 08, 2005
Posts: 18
Thanks Joyce! The conception now is cleared.


I wrote nonsense code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When both Thread and Runnable have public void run
 
Similar Threads
Benefits of "extends Thread" vs "implements Runnable"
How setDaemon works
Creating threads using inner classes
What is the output of the program?please explain....
Sleep, main thread