NonStop Server for Java 4.2 Programmer's Reference
Multithreaded Programming
The Java virtual machine for the NonStop Server for Java 4 is multithreaded. It uses Standard POSIX Threads
(product number T1248), which conforms to IEEE POSIX Standard 1003.lc. Threads are scheduled for execution
by the Standard POSIX Threads library, not by the operating system. All threads created within a process share the
same process address space in the same CPU. With Standard POSIX Threads, one thread can never be preempted
by another thread.
The topics in this discussion are:
Thread Scheduling●
Threading Considerations for Java Code●
Threading Considerations for Native Code●
Thread Scheduling
The Java runtime supports a simple, deterministic, scheduling algorithm known as fixed-priority scheduling. The
Java runtime does not time-slice.
For the NonStop system, the thread-scheduling algorithm is not preemptive; that is, a thread continues to run until
it explicitly yields or otherwise causes a yield by invoking a blocking operation on the thread.
When a thread gives up control, the runnable threads of the highest priority are run in first-in-first-out order. A
lower priority thread is run (also in first-in-first-out order) only when no runnable threads of a higher priority are
available. Where no runnable user threads are available, an internal NULL thread is run. This NULL thread wakes
up and gives control to other threads on events, such as signals, timer completions, and I/O completions.
When a Java thread is created, the thread inherits its priority from the thread that created it. The priority of the
thread varies from MIN_PRIORITY (1) to MAX_PRIORITY (10), where the default priority is
NORM_PRIORITY (5). After a thread is created, the setPriority method can be used to alter the priority of
the thread.
The Java virtual machine threads use predetermined priorities, some of which are higher than the priority of any of
the user threads. By default, the-XX:+UseThreadPriorities option is true and any attempt to alter the
option has no effect. Although attempts to use XX: options, which affect thread priorities, might be accepted at
the command line, these options have no effect when used on the NonStop system.
A selfish thread (a thread that executes in a tight loop without giving up control) could, theoretically, run forever.
However, after a while, the operating system will periodically reduce the priority of the process in stages, until its
priority reaches a very low value.
Timers are never guaranteed to be exact. Invocation of timer callbacks and detection of I/O completions can be
severely impacted by long-running threads.
For a demonstration of scheduling on a NonStop system, review the output of the following program and its results
when run:
RaceDemo.java
public class RaceDemo {