Adjusting the priority of threads in Java
2007-08-23 12:14:10
With the next code we'll be able to control the priority our Java program's threads run.
Thread me = Thread.currentThread();
me.setPriority(Thread.MIN_PRIORITY);
For the example we've used the minimun priority, so every other process will have preference for CPU time but this. To check the others priority levels, look at the Thread object with an object inspector, or use an IDE with the ability of showing avaliable methods as you program, like Eclipse.
There's a problem: JDK has 10 priority levels, while every OS has its own number. For example, Windows NT has seven and is not uncommon for a Unix system to be in the hundreds. The best approach is to stick to MAX_PRIORITY, NORM_PRIORITY and MIN_PRIORITY within the code.