Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread problem

 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


SIr i have a doubt that

the above code is giving gogogo as the output but sir it is calling the method run() of the thread class ...how it is giving this..
 
Javier Cortes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's perfectly valid to call the run method just like any other regular public method. However, it's important to understand that a new thread is not being created while calling run. It is simply the main thread (the tread running the main method) who is calling run on the t object. That's what's happening on the first 2 calls of run (hence, the first two "go"). On t.start() a new thread is being created, and once the JVM selects it to be on the Runnable state it executes the run method (thus, the last "go"). Also, the Thread constructor you are using is setting the target to Cruiser object. You're specifically stating the object whose run method is called.
 
Henry Wong
author
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
narendra bhattacharya wrote:
the above code is giving gogogo as the output but sir it is calling the method run() of the thread class ...how it is giving this..


The default run() method, is to check for a runnable object, and then to call it. This is why when you call the thread start() method, it will eventually get to your runnable objects run() method.

Henry
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually we are running three Threads tow of Thread class and one of main() method thread
So when we start the Thread by it runs Three Threads and thus the output is gogogo.
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it runs Three Threads and thus the output is gogogo.


there are just two threads the 1. main 2.from ThreadClass.
first and second go is printed by simple method call t.run() (it has nothing to do with threading concept).
third go is printed when start method calls run of thread.

-P
 
Javier Cortes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praful Thakare wrote:
it runs Three Threads and thus the output is gogogo.


there are just two threads the 1. main 2.from ThreadClass.
first and second go is printed by simple method call t.run() (it has nothing to do with threading concept).
third go is printed when start method calls run of thread.

-P


...as stated on the second post. However, I'd like to correct that I meant "...and once the JVM selects it to be on the Running state..." instead of "...and once the JVM selects it to be on the Runnable state...". Sorry for that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic