--- old/src/java.base/share/native/libjli/java.c 2016-04-23 21:12:29.145228500 +0900 +++ new/src/java.base/share/native/libjli/java.c 2016-04-23 21:12:29.050228500 +0900 @@ -125,6 +125,7 @@ static void PrintUsage(JNIEnv* env, jboolean doXUsage); static void ShowSettings(JNIEnv* env, char *optString); static void ListModules(JNIEnv* env, char *optString); +static void SetNativeThreadName(JNIEnv* env, char *name); static void SetPaths(int argc, char **argv); @@ -325,6 +326,11 @@ * mainThread.isAlive() to work as expected. */ #define LEAVE() \ + SetNativeThreadName(env, "DestroyJavaVM"); \ + if ((*env)->ExceptionOccurred(env)) { \ + /* Clear non critical pending exceptions at this point. */ \ + (*env)->ExceptionClear(env); \ + } \ do { \ if ((*vm)->DetachCurrentThread(vm) != JNI_OK) { \ JLI_ReportErrorMessage(JVM_ERROR2); \ @@ -386,6 +392,12 @@ exit(1); } + SetNativeThreadName(env, "main"); + if ((*env)->ExceptionOccurred(env)) { + /* Clear non critical pending exceptions at this point. */ + (*env)->ExceptionClear(env); + } + if (showSettings != NULL) { ShowSettings(env, showSettings); CHECK_EXCEPTION_LEAVE(1); @@ -1686,6 +1698,29 @@ joptString); } +/** + * Set native thread name if possible. + */ +static void +SetNativeThreadName(JNIEnv *env, char *name) +{ + jmethodID currentThreadID, setNativeNameID; + jobject currentThread; + jstring nameString; + jclass cls; + + NULL_CHECK(cls = FindBootStrapClass(env, "java/lang/Thread")); + NULL_CHECK(currentThreadID = (*env)->GetStaticMethodID(env, cls, + "currentThread", "()Ljava/lang/Thread;")); + NULL_CHECK(currentThread = (*env)->CallStaticObjectMethod(env, cls, + currentThreadID)); + NULL_CHECK(setNativeNameID = (*env)->GetMethodID(env, cls, + "setNativeName", "(Ljava/lang/String;Z)V")); + NULL_CHECK(nameString = (*env)->NewStringUTF(env, name)); + (*env)->CallVoidMethod(env, currentThread, setNativeNameID, + nameString, JNI_TRUE); +} + /* * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java */