Java on Solaris 7 Developer's Guide

thr_setconcurrency(3T) Example

A compute-bound application such as parallelized matrix multiplication must use a native method to callthr_setconcurrency(3T) . This insures that sufficient concurrency resources are available to the Java application to fully use multiple processors. This is not necessary for most Java applications and applets. The following code is an example of how to do this.

The first element is the Java application, MPtest.java, that will use MPtest_NativeTSetconc(). This application creates 10 threads, each of which displays an identifying line, then loops 10,000,000 times to simulate a compute-bound activity.


Example 3-3 MPtest.java


import java.applet.*;
import java.io.PrintStream;
import java.io.*;
import java.net.*;
 
class MPtest {
   static native void NativeTSetconc();
   static public int THREAD_COUNT = 10;
   public static void main (String args[]) {
      int i;
 
// set concurrency on Solaris - sets it to
// sysconf (_SC_ NPROCESSORS_ONLN)
      NativeTSetconc();
// start threads
      client_thread clients[] = new client_thread[ THREAD_COUNT ];
      for ( i = 0; i < THREAD_COUNT; ++i ){
         clients[i] = new client_thread(i, System.out);
         clients[i].start();
      }  
   }
 
   static { System.loadLibrary("NativeThreads"); 
}
class client_thread extends Thread {
   PrintStream out;
   public int LOOP_COUNT = 10000000;
   client_thread(int num, PrintStream out){
      super( "Client Thread" + Integer.toString( num ) );
      this.out = out;
      out.println("Thread " + num);
   }
   public void run () {
      for( int i = 0; i < this.LOOP_COUNT  ; ++i ) {;
				}
		}
}

The second element is the C stub file, MPtest.c, generated from MPtest.java by the utility javah(1). Enter


% javah -stubs MPtest.java

The third element is the C header file, MPtest.h, also generated from MPtest.java by the utility javah(1). Enter


% javah MPtest.java

The fourth element is the C function, NativeThreads.c, which performs the call to the C library interface.


#include <thread.h>
#include <unistd.h>
#include <jni.h>
JNIEXPORT void JNICALL Java_MPtest_NativeTSetconc(JNIEnv *env, jclass obj) {
		thr_setconcurrency(sysconf(_SC_NPROCESSORS_ONLN));
}

Finally, combining the four files into the Java application, MPtest.class, is most easily done with amake(1S) file such as


Example 3-4 MPtest.class


# Make has to be done in two stages:
# first do "make MPtest"
# Then create NativeThreads.c to incorporate the native call
# to "thr_setconcurrency(_SC_NPROCESSORS_ONLN)"
# and then do "make lib".
# After this, you should be able to run "java MPtest" with LD_LIBRARY_PATH
# and CLASSPATH set to "."
JAVA_HOME=/usr/java JH_INC1=${JAVA_HOME}/include JH_INC2=${JAVA_HOME}/include/solaris
CLASSPATH=.; 
export CLASSPATH;
MPtest:
${JAVA_HOME}/bin/javac MPtest.java 
(CLASSPATH=.; 
export CLASSPATH; 
${JAVA_HOME}/bin/javah MPtest)
(CLASSPATH=.; 
export CLASSPATH; 
${JAVA_HOME}/bin/javah -jni MPtest)cc -G -I${JH_INC1} -I${JH_INC2} NativeThreads.c\
		 -lthread -o libNativeThreads.so
clean:
rm -rf *.class libNativeThreads.so NativeThreads.o *.h