This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Thread Priorities Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Priorities" Watch "Thread Priorities" New topic
Author

Thread Priorities

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

If I have a thread with a priority of 10 and another with a priority of only 1, is it possible for the thread with the highest priority to dominate to the point that the thread with the lower priority isn't even called -- I understand that instead of yield(), I should use sleep().

-- Kaydell
Praveen Balaji
Ranch Hand

Joined: Jun 17, 2001
Posts: 60
Short answer: It's possible. It's called Thread Starvation.

You may want to google for

1. Thread Starvation
2. Thread Busy Wait

I believe you are exploring threads (considering you posted 3 questions). Might be a good idea to invest in a book. I dont know which one, but someone should be able to point you to a good book.

I always find Sun Trails a nice place to start reading about any java features/libraries. You get a broad idea of what is offered and what you should look (google) for.
[ November 24, 2006: Message edited by: praveen balaji ]
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Can I prevent thread starvation by leaving all threads at a normal priority of 5?

-- Kaydell
Sanjit Kumar
Ranch Hand

Joined: Dec 04, 2006
Posts: 35
It depends on the system how it handles threads.

If it uses FCFS(First Come First Serve) algorithm, you cant avoid starvation(setting all thread of priority 5 or same ).

If system uses RR(Round Robin) algorithm to handle multiple threads no thread starvation will occur(Limitation: this algorithm affects efficency as scheduler will spent most of the time to switch from one thread to another thread in case of very large number of threads).

Correct me for my wrong concepts if there is any in above explanation...........
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

This is the choice of the OS. Priority is not exclusivity. An good OS will never starve a thread no matter its priority.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Kaydell Leavitt:
If I have a thread with a priority of 10 and another with a priority of only 1, is it possible for the thread with the highest priority to dominate to the point that the thread with the lower priority isn't even called.

Definitely can, only testing will give meaningful answers. For:
Originally posted by praveen balaji
but someone should be able to point you to a good book.
That book is defintely Java Threads, 3rd Edition by Scott Oaks & Henry Wong. I have lots of books, this is an exceptionally good book. Thread starvation, which is the question you are asking, depends on the operating system, the workload and a compiler. Generally, a Thread aware os will silently boost the priority of a lower priority thread if it needs that thread to complete a task; but in the more general case setting a priority of ten will make that thread seem to be the entire machine as long as there is something for it to do.

Can I prevent thread starvation by leaving all threads at a normal priority of 5?
Well yes, but what is the problem you are trying to make an approach to ?

As for sleep() v yield() - I asked about that and use the sleep(0) syntax but I was told that yield() is implemented in the Java Engine, therefore it would be a personal preference which one to use.
[ December 17, 2006: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Priorities