Name: boT120536 Date: 03/07/2001
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
When a native method creates a thread, if the native method is called by a java
Runnable object which was started by javax.swing.SwingUtilities.invokeLater or
javax.swing.SwingUtilities.invokeAndWait, the native thread takes several
seconds to start.
To reproduce the problem:
1. compile jnitest.java with javac
2. build the C header file with javah
3. build jnitest.c into a native library (cl -LD -MT jnitest.c)
4. run the example with this command line:
java jnitest RunType
...where RunType is how the Runnable object should be executed
R = use Runnable.run
T = use the Thread object
L = use javax.swing.SwingUtilities.invokeLater
W = use javax.swing.SwingUtilities.invokeAndWait
e.g. java jnitest W
If the parameter is L or W, the thread will take several seconds to start.
// this is jnitest.java
class jnitest
{
static native void count(int num);
static
{
System.loadLibrary("jnitest");
}
public static void main(String[] args) throws Exception
{
Runnable run =
new Runnable()
{
public void run()
{
long t1,t2;
t1 = System.currentTimeMillis();
count(0);
t2 = System.currentTimeMillis();
System.out.println("time = " + (t2 - t1));
System.exit(0);
}
};
switch(args[0].charAt(0))
{
case 'R':
run.run();
break;
case 'L':
javax.swing.SwingUtilities.invokeLater(run);
break;
case 'W':
javax.swing.SwingUtilities.invokeAndWait(run);
break;
case 'T':
(new Thread(run)).start();
break;
}
}
}
// end of jnitest.java
/* this is jnitest.c */
#include "jnitest.h"
#include <windows.h>
#include <time.h>
void myThread(void *p)
{
*((char *)p) = 'A';
}
JNIEXPORT void JNICALL Java_jnitest_count(JNIEnv *env, jclass t, jint count)
{
int rc;
clock_t clockStart;
char buffer = ' ';
rc = (int)_beginthread( myThread, 5000, &buffer ) ;
clockStart = clock() ;
while ( buffer == ' ' )
{
Sleep( 0 ) ;
}
puts("DONE");
}
/* end of jnitest.c */
(Review ID: 117945)
======================================================================