8
I intend to use thread priorities within my Java code. The application shall run on my Linux system:
>uname -a
Linux <host> 3.0.0-15-generic #26-Ubuntu SMP <date> x86_64 x86_64 x86_64 GNU/Linux
>java -version
java version "1.6.0_23"
OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-0ubuntu1.11.10.1)
OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)
After some reading in the Web I start my test-application with the following command now:
sudo java -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=1 -jar ThreadPriorityTest.jar
The test-application consists of the following two classes:
package ch.mypackage;
public class CountingRunnable implements Runnable {
private long count = 0;
private boolean goOn = true;
public long getCount() {
return count;
}
public void stop() {
goOn=false;
}
public void run() {
for(long iteration=0;goOn&&iteration<Long.MAX_VALUE;++iteration) {
++count;
}
}
}
package ch.mypackage;
public class PriorizedCountingThreads {
private static final int NUM_MILLIS_TO_COUNT_FOR = 1*60*1000;
private static CountingRunnable[] runnables;
private static Thread[] threads;
public static void main(String[] args) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
System.out.println("MIN_PRIORITY: "+Thread.MIN_PRIORITY);
System.out.println("MAX_PRIORITY: "+Thread.MAX_PRIORITY);
int numPriorityLevels = (Thread.MAX_PRIORITY-Thread.MIN_PRIORITY) + 1;
init(numPriorityLevels);
startThreads();
try {
Thread.sleep(NUM_MILLIS_TO_COUNT_FOR);
} catch (InterruptedException e) {
e.printStackTrace();
}
stopRunnables();
printCounts();
}
private static void p