NonStop Server for Java 7.0 Programmer's Reference
process address space in the same CPU. With PUT library on NonStop systems, one thread can
never be preempted by another thread if the -XX:ThreadTimeSlice option is not used.
This section includes the following topics:
• “Thread scheduling” (page 41)
• “Threading considerations for Java code” (page 43)
• “Threading considerations for native code” (page 43)
• “ThreadDumpPath support” (page 44)
Thread scheduling
When a Java thread is created, the thread inherits its priority from the parent thread. The priority
of the thread varies from MIN_PRIORITY to MAX_PRIORITY, where the default priority is
NORM_PRIORITY. After a thread is created, the setPriority method can be used to alter the
priority of the thread.
Java application developers must be aware of the following consideration while designing or
enabling multi-threaded Java applications for Nonstop systems:
• A selfish thread is a thread that executes in a tight loop without giving up control either by
explicitly yielding or by invoking a blocking operation.
• Since the thread-scheduling algorithm on Nonstop is non-preemptive and does not time slice,
it is possible for such a thread to prevent other threads from getting access to CPU time.
Theoretically, such a thread can run forever. However, after a while, the operating system
periodically reduces the priority of the process in stages, until its priority reaches a very low
value.
For a demonstration of scheduling on a NonStop system, review the results and output of the
following programs:
RaceDemo.java
public class RaceDemo {
private final static int NUMRUNNERS = 2;
public static void main(String[] args) {
SelfishRunner[] runners=new SelfishRunner[NUMRUNNERS];
for (int i = 0; i < NUMRUNNERS; i++) {
runners[i] = new SelfishRunner(i);
runners[i].setPriority(2);
}
for (int i = 0; i < NUMRUNNERS; i++)
runners[i].start();
}
}
SelfishRunner.java
public class SelfishRunner extends Thread {
private int tick = 1;
private int num;
public SelfishRunner(int num) {
this.num = num;
}
public void run() {
while (tick < 400000) {
tick++;
if ((tick % 50000) == 0)
System.out.println("Thread #"+num+", tick = "+tick);
Multi-threaded programming 41










