Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can behavior be guaranteed using priority and yield?

 
Myyron Murray Latorilla
Ranch Hand
Posts: 65
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20991
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Myyron Murray Latorilla
Ranch Hand
Posts: 65
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic