• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Priorities

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

-- Kaydell
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...........
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic