File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Can behavior be guaranteed using priority and yield? 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 » Java » Threads and Synchronization
Bookmark "Can behavior be guaranteed using priority and yield?" Watch "Can behavior be guaranteed using priority and yield?" New topic
Author

Can behavior be guaranteed using priority and yield?

Myyron Murray Latorilla
Ranch Hand

Joined: Dec 10, 2009
Posts: 65

Hi guys, just need a confirmation on this one.

If for example I have 2 threads having 1 using MAX_PRIORITY and the other MIN_PRIORITY, is it guaranteed that the thread with MAX_PRIORITY will always be in running state and run to completion first? I tested this on my machine and the result is the one with MAX_PRIORITY always get to execute even after yield invokation on each iteration. I just want to have this behavior confirmed.

Thanks in advance.


OCPJP6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18543
    
  40

myyron latorilla wrote:
If for example I have 2 threads having 1 using MAX_PRIORITY and the other MIN_PRIORITY, is it guaranteed that the thread with MAX_PRIORITY will always be in running state and run to completion first? I tested this on my machine and the result is the one with MAX_PRIORITY always get to execute even after yield invokation on each iteration. I just want to have this behavior confirmed.


A few things to note:

1. Whether an application is in a runnable state has NOTHING to do with the priority. If the application is reading from a disk, it will be in the block state waiting for the disk drive to fetch the data. It will not be able to run any code until it has the data -- regardless of priority. Two threads with different priorities but doing lots of IO, is probably more dependent on the disk than priority.

2. The priority affects only the runnable state. It also doesn't affect the starting thread. A low priority thread can start first, and get a head start, until the higher priority thread starts (and is in the runnable state).

3. A higher priority is only important if the OS has to choose between two runnable threads. A high priority thread that is not in a runnable state (like waiting for disk) will give up the CPU to allow another thread to run. If there is enough I/O, there could be enough cpu cycles for the processor to service both threads. It may be possible for the CPU to not have to choose.

4. Some operating systems, like Windows, have thread starvation prevention code to favor lower priority threads from starving (if the higher priority thread has been running for a while).

5. A thread can only use one core. And many computers these days are multiple core machines. So, many computers can easily run both the lower priority and higher priority thread at the same time. Again, the OS may not have to choose.

The problem with this question is the word "GUARANTEED", and given all of these things to note, are you certain that you can't build a case where the lower priority threads can sometime win?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Myyron Murray Latorilla
Ranch Hand

Joined: Dec 10, 2009
Posts: 65

Thanks, those were helpful notes, I guess there is no guarantee given those. I could simulate 3, 4, 5, but your answer is enough to make the answer as "not guaranteed".
 
wood burning stoves
 
subject: Can behavior be guaranteed using priority and yield?
 
Similar Threads
threads and priority - guarenteed?
What is the differnt between Thread Sleep n Thread No sleep
yield() - method
Thread.yield question for the Cowgirl
Dan's Threads mock exam query