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 Threads doubt?? 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 "Threads doubt??" Watch "Threads doubt??" New topic
Author

Threads doubt??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Guys,

Consider the code below that I tried,


If I uncomment the last 3 lines, it prints
start from thread class
run from runnable class

But at the same time, I have implemented the start method in the Runnable anonymous class and when I say t.start(), why does the run method run??

Any ideas??


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381

Here the start method of the Thread class is not overriden.So calling of the start of the Thread class will inturn call the run method.

overriden the start method of Thread class.So start will be called.
class jothi extends Thread{

Java Naming Convention violation....


~Sanjeev Singh<br />SCJP 1.5
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Sanjeev,

My question is why even after overriding the start method in the the anonymous runnable class makes the run method is invoked and why not this happens with the thread class??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Originally posted by Jothi Shankar Kumar Sankararaj:
Sanjeev,

My question is why even after overriding the start method in the the anonymous runnable class makes the run method is invoked and why not this happens with the thread class??


When you overriden the start() method of the Thread class, and then called that start() method, it will run your new start() method, and not actually start any threads -- hence, run() method will not be called.

When you created a start() method, in your anonymous runnable, you actually didn't override anything, as a runnable doesn't have a start() method. Furthermore, you didn't override the start method of the thread class, so when you called it, it creates a new thread, which calls the run() method of the runnable class.

Henry
[ December 22, 2006: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Jothi,

My question is why even after overriding the start method in the the anonymous runnable class makes the run method is invoked and why not this happens with the thread class??

There is no method named start in Runnable interface.In the first ex you are calling the start method of Thread not of the runnable target.For more essence please see the API.
[ December 22, 2006: Message edited by: Sanjeev Kumar Singh ]
av bharathiraja
Greenhorn

Joined: Aug 24, 2006
Posts: 7
Folks!
Still im not clear. as per my understanding t refers anonymous and j refers obj of jothi.

When we uncomment the last method it produces the foll. o/p
Start from thread class
run from runnable class

and when I put a comment on the line t.start(); it gives

start from thread class instead of invking methods from anonymous(runnable) class. And how the methods from anonymous class got invoked in in the first o/p. and Why it is invoking from two different classes in different situations.


Regards<br />AV
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

and when I put a comment on the line t.start(); it gives

start from thread class instead of invking methods from anonymous(runnable) class. And how the methods from anonymous class got invoked in in the first o/p. and Why it is invoking from two different classes in different situations.


When you comment "t.start()", nothing should happen. The "start from thread class" message is from the "j.start()". Basically, if you won't call start(), then the question of whether the start() method will call the run() method is moot.

Henry
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Henry Excellent reply.


/** Code speaks louder than words */
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Threads doubt??
 
Similar Threads
java threads
Thread doubt
Overriding the start method of Thread Class
Thread Doubt
Thread question - uCertify 6.0