aspose file tools
The moose likes Threads and Synchronization and the fly likes Randomness of 'the thread scheduler' Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Randomness of Watch "Randomness of New topic
Author

Randomness of 'the thread scheduler'

ce orto
Greenhorn

Joined: Jan 30, 2009
Posts: 3
Below code should execute printlns (one from main() stack, other from run() stack) randomly (according to page 498, HF Java), but it does NOT. Does this have anything to do with Core2Duo™ processor? Besides, the original code (page 494) has MyRunnable public and ThreadTester not (I've turned that around so it "works" ): is this a mistake or am I missing something?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16810
    
  19

Below code should execute printlns (one from main() stack, other from run() stack) randomly (according to page 498, HF Java),


Really?!? Does the book really say the order is random? Or does the book say that the order is undefined?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

ce orto wrote: Besides, the original code (page 494) has MyRunnable public and ThreadTester not (I've turned that around so it "works" ): is this a mistake or am I missing something?


The source file has to be named after whichever class you make public, but otherwise, the program is correct either way. It would also be correct if neither class were public.

And as Henry implies, if the book says the order is "random", it's just being imprecise. The order is undefined and (on most Java implementations) technically indeterminate; although for any given implementation, it's quite likely that virtually all runs will print these lines in the same order.


[Jess in Action][AskingGoodQuestions]
ce orto
Greenhorn

Joined: Jan 30, 2009
Posts: 3
Henry Wong wrote:
Below code should execute printlns (one from main() stack, other from run() stack) randomly (according to page 498, HF Java),


Really?!? Does the book really say the order is random? Or does the book say that the order is undefined?

Henry


»[Two arrows pointing to the console output] Notice how the order changes randomly. Sometimes the new thread finishes first, and sometimes the main thread finishes first.«

Ernest Friedman-Hill wrote:
ce orto wrote: Besides, the original code (page 494) has MyRunnable public and ThreadTester not (I've turned that around so it "works" ): is this a mistake or am I missing something?


The source file has to be named after whichever class you make public, but otherwise, the program is correct either way. It would also be correct if neither class were public.

And as Henry implies, if the book says the order is "random", it's just being imprecise. The order is undefined and (on most Java implementations) technically indeterminate; although for any given implementation, it's quite likely that virtually all runs will print these lines in the same order.

Thank you. I was just curious, whether there is any pattern in the low-level process that yields such undefined (random ) order.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

ce orto wrote:
Thank you. I was just curious, whether there is any pattern in the low-level process that yields such undefined (random ) order.


It totally depends on the JVM implementation. Many (but not all) JVMs use the host OS's thread facility these days. On Windows, I believe one JVM thread corresponds to one OS thread, so it's completely up to the Windows thread scheduler. On Solaris, the JVM uses LWPs ("fibers") which are like sub-threads within a thread; I think it's up to the JVM to do scheduling among LWPs (although again, I'm not sure, these things change over time.) Henry might have more up-to-date information on specific implementations. But in any case, the short answer is you really can't tell what's going to happen; you should design the program for any contingency.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Randomness of 'the thread scheduler'
 
Similar Threads
Threads doubt??
java threads
Thread question - uCertify 6.0
Thread Doubt
Threads