Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Priorities

 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I prevent thread starvation by leaving all threads at a normal priority of 5?

-- Kaydell
 
Sanjit Kumar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic