Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I consider changing thread priority

I once was asked to increase thread priority to fix a problem. I refused, saying that changing it was dangerous and was not the root cause of the problem.

My question is, under what circumstannces should I conider changing priority of threads?

like image 582
theschmitzer Avatar asked Sep 18 '08 18:09

theschmitzer


People also ask

Can we change the thread priority if yes then how?

Then a Thread object can be created and the start() method called. The thread priority determines when the processor is provided to the thread as well as other resources. It can be changed using the method setPriority() of class Thread.

Who decide the thread priority?

Explanation: Thread scheduler decides the priority of the thread execution.

What happens if two threads having same priority are started?

If two threads of the same priority are waiting for the CPU, the scheduler arbitrarily chooses one of them to run. The chosen thread runs until one of the following conditions is true: A higher priority thread becomes runnable. It yields, or its run method exits.

What is the purpose of assigning priorities for threads?

Thread priority in Java is a number assigned to a thread that is used by Thread scheduler to decide which thread should be allowed to execute. In Java, each thread is assigned a different priority that will decide the order (preference) in which it is scheduled for running.


1 Answers

When you've made a list of the threads you're using and defined a priority order for them which makes sense in terms of the work they do.

If you nudge threads up here and there in order to bodge your way out of a problem, eventually they'll all be high priority and you're back where you started. Don't assume you can fix a race condition with prioritisation when really it needs locking, because chances are you've only fixed it in friendly conditions. There may still be cases where it can fail, such as when the lower-priority thread has undergone priority inheritance because another high-priority thread is waiting on another lock it's holding.

If you classify threads along the lines of "these threads fill the audio buffer", "these threads make my app responsive to system events", "these threads make my app responsive to the user", "these threads are getting on with some business and will report when they're good and ready", then the threads ought to be prioritised accordingly.

Finally, it depends on the OS. If thread priority is completely secondary to process priority, then it shouldn't be "dangerous" to prioritise threads: the only thing you can starve of CPU is yourself. But if your high-priority threads run in preference to the normal-priority threads of other, unrelated applications, then you have a broader responsibility. You should only be raising priorities of threads which do small amounts of urgent work. The definition of "small" depends what kind of device you're on - with a 3GHz multi-core processor you get away with a lot, but a mobile device might have pseudo real-time expectations that user-level apps can break.

Keeping the audio buffer serviced is the canonical example of when to be high priority, though, since small under-runs usually cause nasty crackling. Long downloads (or other slow I/O) are the canonical example of when to be low priority, since there's no urgency processing this chunk of data if the next one won't be along for ages anyway. If you're ever writing a device driver you'll need to make more complex decisions how to play nicely with others.

like image 160
Steve Jessop Avatar answered Sep 24 '22 06:09

Steve Jessop