• 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

Threads doubt??

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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....
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
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 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 ]
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Henry Wong
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

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
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry Excellent reply.
 
reply
    Bookmark Topic Watch Topic
  • New Topic