• Post Reply Bookmark Topic Watch Topic
  • New Topic

threads in java  RSS feed

 
Sanjiv Saha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In this case when I execute the code it prints -- In SpecialRunnable run!!! and I call run() in main() even though run() is not present in SpecialThread class..please explain the flow
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread class implements Runnable which comes with the run() method. Hence SpecialThread has the run() method because it is extending the Thread class. But just calling run() will not spawn a new thread. For spwaning a new thread you would have to invoke the start() method of the Thread.

I would recommend you to go through the Java tutorials on Threads to get an understanding of Threads and its concepts in Java.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is exactly the same as what Mike J. Thompson said in your other topic, except that you are not starting a separate thread at all. You are just directly calling the run() method in class Thread, which does this:

So, it calls the run() method of the target, which is SpecialRunnable.run() because you passed an instance of SpecialRunnable to the constructor.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!