» Back to table of contents

using threads

The HP-UX 10.20 operating system historically supported a user-threads package known as DCE or CMA threads. The user-threads package contained in the HP-UX Virtual Machine with JIT for Java™ (JVM) for HP-UX 10.20 for Java™ 1.1 is known as "green" threads and is similar to but not the same as CMA threads. With user-threads packages such as CMA or green threads, you can create more than one thread in a user's environment. However, the user threads in a single process can not run on two processors at the same time. The 10.20 kernel will only schedule the process, not individual threads.

The HP-UX 11.0 operating system supports kernel threads. Each kernel thread is scheduled to run independently which can result in multiple threads running in parallel on multiple processors. The current JVM for HP-UX 11.0 uses the POSIX 1003.1x Kernel Threads API to access thread support on HP-UX 11.0. The libpthread library is in /usr/lib.

The rest of this chapter focuses on threads information specific to HP's implementation of Java™ for HP-UX 10.20 and HP-UX 11.0. For additional information on threads, read ThreadTime: The Multithreaded Programming Guide" by Scott J. Norton and Mark DiPasquale, and "Programming with POSIX(R) Threads" by David R. Butenhof. Other information on threads can be found with the "search" link at the HP documentation website.

running the kernel-threaded or green-threaded JVM on HP-UX 11.0

NOTE: Green threads is not included in the HP-UX SDK, for the Java™ 2 Platform for HP-UX 11.0 or the HP-UX Runtime Environment, for the Java™ 2 Platform.

Users with the HP-UX Virtual Machine with JIT for Java™ (JVM) for HP-UX 11.0 have the choice of running the kernel-threaded or the green-threaded JVM. By default, the java command for HP-UX 11.0 uses the kernel-threaded JVM.

You can override the default by setting the THREADS_FLAG environment variable to "green":

In Korn shell:

export THREADS_FLAG=green
java MyJavaApplication

In C shell:

setenv THREADS_FLAG green
java MyJavaApplication

You may also override the default by passing the -green option to the java, jdb, javac or javap commands.

Setting the THREADS_FLAG environment variable to "native" or passing the -native option to the java, jdb, javac or javap commands forces the use of the kernel-threaded JVM.

The shell scripts in /opt/java/bin choose between the native and green versions based on command-line arguments and variables. Your system administrator can modify the .java_wrapper shell script in the /opt/java/bin directory to change the default from "native" to "green."

The command line argument -green or -native takes precedence over the setting of the THREADS_FLAG environment variable and the shell script.

The green-threaded JVM installs in /opt/java/bin/PA_RISC/green_threads and /opt/java/lib/PA_RISC/green_threads. The kernel-threaded JVM installs in /opt/java/bin/PA_RISC/native_threads and /opt/java/lib/PA_RISC/native_threads.

The Java™ Native Interface Documentation in "Using JNI" includes information on any changes to compiler or linker flags necessary for building shared libraries of Java™ native methods for HP-UX 11.0.

thread scheduling issues on hp-ux 11.0

Due to security restrictions in HP-UX 11.0, if the JVM is not running as a process having privilege PRIV_RTSCHED, then all Java™ threads within that JVM will execute at the same priority regardless of the priorities set by the programmer via java.lang.Thread.setPriority(). If the JVM is running as a user with PRIV_RTSCHED, Java™ thread priorities will be mapped into the range of priorities offered by the SCHED_TIMESHARE policy (see rtsched(2) for details).

Because threads from different processes, perhaps executing on behalf of different users, are competing for the same computational resources, the HP-UX scheduler adjusts the priorities of threads to prevent a high-priority, compute-bound thread from locking out lower priority threads. This adjustment of priorities is a requirement for a multi-user system. However, the Java™ language assumes a scheduling policy where thread priorities are only changed explicitly by the programmer. The kernel-threaded JVM uses the HP-UX scheduler and thus the priorities of Java™ threads may change based on their CPU utilization.

developing hybrid applications

Developers who produce hybrid applications where the application entry point is in a language other than Java™, for example a C language main, need to link their applications with the libpthread.sl library in order to use the kernel-threaded JVM. Failing to link with libpthread.sl will result in a run-time failure when the JVM is loaded. Since the libpthread library was not available in releases prior to HP-UX 11.0, developers who wish to support 10.20 and also wish to take advantage of the kernel-threaded JVM on HP-UX 11.0 and later versions need to release multiple versions of their applications (one for 10.20 and one for 11.0).

When building native method implementations to be used with the kernel-threaded JVM, sources must be compiled with the following additional command-line options:

-DNATIVE -D_POSIX_C_SOURCE=199506L

Additionally, hybrid applications must link with the pthread library using -lpthread on the link line.

The Java™ Native Interface documentation in "Using JNI" also includes a section describing how developers of hybrid applications can take advantage of the kernel-threaded JVM along with a description of the compiler and linker options necessary to build the application.

limitations of the green-threaded JVM

The green-threaded (user-level) Java™ threads implementation on HP-UX requires the JVM to interrupt and redirect certain UNIX system calls. The set of redirected system calls currently includes accept(), close(), create(), dup(), fcntl(), getmsg(), open(), pipe(), poll(), putmsg(), read(), readv(), recv(), recvfrom(), select(), send(), socket(), write(), and writev(). This may cause undesirable effects on libraries that also depend on the ability to redirect these system calls. As a result, the green-thread based JVM cannot interoperate with other libraries which provide user-level threads (e.g. CMA threads for DCE).

You cannot attach a native thread to the green-threaded JVM -- AttachCurrentThread() simply fails.

You cannot unload the green-threaded JVM without terminating the process. The DestroyJavaVM() call returns an error code.

These limitations do not exist on the HP-UX 11.0 kernel-threaded JVM.

» Please let us know additional information you'd like to see in the programmer's guide.

Java™ and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. Hewlett-Packard is independent of Sun Microsystems, Inc.