28

I have a program that runs in a few threads. The main thread shares an object with the other threads and in the main I have a call to:

synchronized(obj){
    do stuff
}

I have a suspicion that the main thread is starved and isn't getting access to obj. How do I raise the priority of the main thread or is it already higher than the other threads by default?

Mat
  • 202,337
  • 40
  • 393
  • 406
Guy
  • 14,178
  • 27
  • 67
  • 88
  • 1
    Macarse already dealt with how to change the priority, but perhaps you could paste some more of your code. There may be something else wrong, or a way to get the other threads to spend less time synchronized on obj. – Jon Bright Oct 24 '09 at 13:36
  • how to set priority among 20 threads , since the thread priority are from 1-10 , so that means only 9 threads can be given priorities....?? – anshulkatta Apr 25 '13 at 11:04

5 Answers5

46

You have a setPriority() method in the Thread class.

Check this javadoc.

Setting thread priority to maximum:

public static void main(String args[]) {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    // Your main code.
}
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
Macarse
  • 91,829
  • 44
  • 175
  • 230
  • so you're saying - in every thread I create do setPriority(minimum). Is there a way you know of to set the main thread's priority to maximum? and does it make a difference? – Guy Oct 24 '09 at 13:19
  • I edited my post with how to set max priority to your main thread. I don't think it will make a difference. I really don't know what you are doing but Java threads shouldn't need you to change priorities to avoid starvation. – Macarse Oct 24 '09 at 13:33
  • 4
    how to set priority among 20 threads , since the thread priority are from 1-10 , so that means only 9 threads can be given priorities....?? – anshulkatta Apr 25 '13 at 11:05
  • Just wondering, what happens if, say, 100 threads all have max priority? – ylun.ca Jul 28 '15 at 19:00
  • 4
    @anshulkatta No. You can give multiple threads the same priority. Remember: priority levels are only a hint. It doesn't necessarily promise anything. If you need more than 3 priorities then you're probably over-engineering. – James Watkins Nov 09 '15 at 02:31
  • 6
    @ylun.ca Priority levels are only relative. Giving everything max priority is same as giving everything min priority. – James Watkins Nov 09 '15 at 02:33
9

The series of articles here indicate some complexities in the management of thread priorities on various platforms.

I wonder if your fundamental problem is simply that your worker threads are very CPU intensive and hence rarely reach a point where they would naturally "let go" of the processor (for example by doing some IO or sleeping.) If such is the case then you might include some calls to yield() in those workers, hence giving other Threads more of a chance.

djna
  • 54,992
  • 14
  • 74
  • 117
6

you can use the setPriority() method. For example:

new MyThread("Foo").start(); 
Thread bar = new MyThread("Bar"); 
bar.setPriority( Thread.NORM_PRIORITY + 1 ); 
bar.start();

This gives bar the new priority which should quickly take over Foo

Edit:

To answer your comment, you can set the max priortiy using:

bar.setPriority( Thread.MAX_PRIORITY );
northpole
  • 10,244
  • 7
  • 35
  • 58
3

Increasing the main thread's priority the way Macarse says will probably work. However, you are relying on the platform's preemptive thread scheduler to work properly. You should instead call the Thread.yield() static method in your worker threads when they are done with whatever important sections of code they are running. This is a good habit to get into even when you are using different levels of thread priority.

Vlad Z.
  • 3,401
  • 3
  • 32
  • 60
Lorek
  • 855
  • 5
  • 11
1

As others already answered, you can indeed set a priority to a thread as follows:

Thread.currentThread().setPriority(priority);

But please be aware, in your example, that thread priority has nothing to do in which order threads get access to a synchronized object. Java uses other criteria to give access. See for example this.

Community
  • 1
  • 1
Sigi
  • 59
  • 6