< prev index next >

src/java.base/share/native/libjli/java.c

Print this page

        

@@ -123,10 +123,11 @@
 
 static void PrintJavaVersion(JNIEnv *env, jboolean extraLF);
 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);
 
 static void DumpState();
 static jboolean RemovableOption(char *option);

@@ -323,10 +324,11 @@
  * thread from the one that executed main, even though they are
  * the same C thread.  This allows mainThread.join() and
  * mainThread.isAlive() to work as expected.
  */
 #define LEAVE() \
+    SetNativeThreadName(env, "DestroyJavaVM"); \
     do { \
         if ((*vm)->DetachCurrentThread(vm) != JNI_OK) { \
             JLI_ReportErrorMessage(JVM_ERROR2); \
             ret = 1; \
         } \

@@ -384,10 +386,12 @@
     if (!InitializeJVM(&vm, &env, &ifn)) {
         JLI_ReportErrorMessage(JVM_ERROR1);
         exit(1);
     }
 
+    SetNativeThreadName(env, "main");
+
     if (showSettings != NULL) {
         ShowSettings(env, showSettings);
         CHECK_EXCEPTION_LEAVE(1);
     }
 

@@ -1684,10 +1688,41 @@
     (*env)->CallStaticVoidMethod(env, cls, listModulesID,
                                  USE_STDERR,
                                  joptString);
 }
 
+/**
+ * Set native thread name as 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);
+
+    if ((*env)->ExceptionOccurred(env)) {
+      /*
+       * We can clear pending exception because exception at this point
+       * is not critical.
+       */
+      (*env)->ExceptionClear(env);
+    }
+}
+
 /*
  * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
  */
 static void
 PrintUsage(JNIEnv* env, jboolean doXUsage)
< prev index next >