• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When both Thread and Runnable have public void run

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was curious on what happens if both the runnable and thread had overridden the run method.



Guess what it prints:

executing thread run.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short (and only needed) answer is that Thread implements Runnable and Runnable defines run() therefore Threads needs to implement run().
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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?
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joyce! The conception now is cleared.


I wrote nonsense code.
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic