This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads and run() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads and run()" Watch "Threads and run()" New topic
Author

Threads and run()

Tom Scott
Greenhorn

Joined: Aug 18, 2006
Posts: 21
Hi All,

My understanding was that if I invoke run()from a thread object directly this would be legal but not create a new call stack or a new thread.

So I wrote the following:


And invoked run() as below....



But I got output:

run:
Invocation : 0: current thread = Thread[Thread-0,5,main]
Invocation : 1: current thread = Thread[Thread-1,5,main]
END
BUILD SUCCESSFUL (total time: 0 seconds)


If I take away the for loop delay statement, I can see the third display from the direct run() call in the main thread. If I put the delay in then I only get two lines as above. I expected to see all three displays as below (in maybe a different order) even with the delay.

run:
Invocation : 0: current thread = Thread[main,5,main]
END
Invocation : 1: current thread = Thread[Thread-0,5,main]
Invocation : 2: current thread = Thread[Thread-1,5,main]
BUILD SUCCESSFUL (total time: 0 seconds)

Can anyone explain why this didn't happen?
Thanks.
-Tom
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18541
    
  40

If I take away the for loop delay statement, I can see the third display from the direct run() call in the main thread. If I put the delay in then I only get two lines as above. I expected to see all three displays as below (in maybe a different order) even with the delay.


If you invoke the run() method directly, the thread object will "not create a new call stack or a new thread". However, keep in mind that you did not override the thread object's run() method. The thread object's run() method is not necessaryly your runnable's run() method.

The thread object's run() method which will check the state of the thread, and call runnable's run() method. By putting in the delay, you are making sure that the thread is no longer in a runnable state, and hence, will not call your run() method.

If you want to call the run() method, use "r.run()" -- don't play with the thread's internal methods.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tom Scott
Greenhorn

Joined: Aug 18, 2006
Posts: 21
Fast answer - thanks!

run() method, use "r.run()" -- don't play with the thread's internal methods.


Rest assured I'd only code this to see what happens - like the time I bent Dad's fishing rod a just a little bit too much to see what a big fish would do...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads and run()
 
Similar Threads
Thread problem.
join() method in Thread doesn't work as expected
Regarding Threads
Question in thread
Thread synchronization