aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread problem" Watch "thread problem" New topic
Author

thread problem

narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66


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..


SCJP1.6,SCWCD1.5
Javier Cortes
Greenhorn

Joined: May 26, 2010
Posts: 6
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


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

Joined: Apr 15, 2010
Posts: 91

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.


Thanks & Regards
Sumit Kothalikar
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 637
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


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Javier Cortes
Greenhorn

Joined: May 26, 2010
Posts: 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread problem