• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads doubt??

 
Joe Harry
Ranch Hand
Posts: 10112
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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??
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • 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 Harry
Ranch Hand
Posts: 10112
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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??
 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
av bharathiraja
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Excellent reply.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic