This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Problem with  output of The Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Problem with  output of The Code" Watch "Problem with  output of The Code" New topic
Author

Problem with output of The Code

buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

From the following code , the ds.g() is getting called first after that the thread named ds.f() is being started but according to the code, the thread should get called first.



Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

From the following code , the ds.g() is getting called first after that the thread named ds.f() is being started but according to the code, the thread should get called first.


Not true. Once the new thread is started, it is now running independent of the main thread. So, whether the f() method or the g() method gets called first depends on the processor, the OS, priority, etc. These two threads are now racing each other, and on some OSes, f() will be called first, on others, g() will execute first.

Henry


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

Joined: Jul 03, 2009
Posts: 136

Thank You
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with output of The Code
 
Similar Threads
Beginner: Threads vs. Runnables
Threading and operation
which executes first?
About Exception Handling
Concurrent threads