编程知识 cdmana.com

Analysis of java thread source code based on Hotspot

Basic concepts

Java Threads are actually mapped to the kernel threads of the operating system , therefore Java Threads are basically managed by the operating system . stay Linux In the system , Threads and processes are described in the same structure , But the process has its own independent address space , Multiple threads of the same process share resources .

Briefly explain : This article is based on openjdk 1.8 Conduct

Thread state

Switching conditions for each thread state , And the calling method, as shown in the figure below : image.png Threads have the following states Java The thread state of is Thread.State The code defined in the enumeration is as follows

public enum State {
    // New creation , Not activated 
    NEW,

    // stay jvm  Run in , It may also be waiting for other resources of the operating system 
    RUNNABLE,

    // Blocking , And waiting for the monitor lock 
    BLOCKED,

    // Waiting thread , Waiting for another thread to perform a specific operation 
    WAITING,

    // Deadline waiting ,  You can set the maximum waiting time 
    TIMED_WAITING,

    // end 
    TERMINATED;
}
 Copy code 

Thread creation

  1. Inherit Thread class , The code is as follows :
class PrimeThread extends Thread {
    long minPrime;
    PrimeThread(long minPrime) {
        this.minPrime = minPrime;
    }

    public void run() {
        // compute primes larger than minPrime
        . . .
        }
}

//  Start thread 
PrimeThread p = new PrimeThread(143);
p.start();
 Copy code 
  1. Realization Runable Interface , The code is as follows ( This is generally recommended ):
class PrimeRun implements Runnable {
    long minPrime;
    PrimeRun(long minPrime) {
        this.minPrime = minPrime;
    }

    public void run() {
        // compute primes larger than minPrime
        . . .
        }
}

//  Start thread 
PrimeRun p = new PrimeRun(143);
new Thread(p).start();
 Copy code 

hotspot Source code

JNI Mechanism

JNI yes Java Native Interface Abbreviation , It provides several API Realized Java Communication with other languages ( Mainly C and C++). 33571679_1.gif JNI The applicable scenarios of When we have some old Libraries , Has been used C The language is written , If you want to migrate to Java come up , It's a waste of time , and JNI Can support Java Procedure and C Language library to interact , This eliminates the need for transplantation . Or with hardware 、 The operating system interacts 、 Improve the performance of the program, etc , You can use JNI. One thing to note is that you need to ensure that local code can work in any Java Virtual machine environment . ​

JNI Side effects Once the use JNI,Java The program will be lost Java Two advantages of the platform :

  1. The program is no longer cross platform , To cross platform , The local language part must be compiled and configured in different system environments .
  2. The program is no longer absolutely secure , Improper use of local code may cause the whole program to crash . A general rule is , Calling local methods should be concentrated in a few classes , This reduces Java Coupling with other languages .

for instance There are many operations in this area , You can refer to the following information

www.runoob.com/w3cnote/jni…

Start process

The starting process is as follows Thread  stay  JVM  Process started in .png

Thread start

Java Create thread Thread After instance , It's through start Method to start the thread , Notice execution . stay start Inside the method , It's called start0() This local method . From this method, we can analyze JVM about Thread The underlying implementation of .

public synchronized void start() {
    //  Determine thread state 
    if (threadStatus != 0)
        throw new IllegalThreadStateException();

    //  Add to group 
    group.add(this);

    boolean started = false;
    try {
        //  Start thread 
        start0();
        started = true;
    } finally {
        try {
            if (!started) {
                group.threadStartFailed(this);
            }
        } catch (Throwable ignore) {
            /* do nothing. If start0 threw a Throwable then it will be passed up the call stack */
        }
    }
}

private native void start0();
 Copy code 

start0() It's a local approach , Let's follow JNI The specification can go to hotspot Virtual source code to find java_lang_Thread_start0 This function . The definition is as follows :

/* * Class: java_lang_Thread * Method: start0 * Signature: ()V */
JNIEXPORT void JNICALL Java_java_lang_Thread_start0 (JNIEnv *, jobject);
 Copy code 

By annotating Method: start0 I can guess , stay jvm There may also be start0 This method , So I searched this method again , eureka Thread.c file . You can see that there is a Java_java_lang_Thread_registerNatives() Method , This is used to initialize in Thread.java Binding with other methods , And in Threa.java One of the first static This method is called in the block , Ensure that this method is the first method to be called in class loading . This native The function of the method is to serve other native Method to JVM in . The code is as follows :

static JNINativeMethod methods[] = {
    {"start0",           "()V",        (void *)&JVM_StartThread},
    {"stop0",            "(" OBJ ")V", (void *)&JVM_StopThread},
    {"isAlive",          "()Z",        (void *)&JVM_IsThreadAlive},
    {"suspend0",         "()V",        (void *)&JVM_SuspendThread},
    {"resume0",          "()V",        (void *)&JVM_ResumeThread},
    {"setPriority0",     "(I)V",       (void *)&JVM_SetThreadPriority},
    {"yield",            "()V",        (void *)&JVM_Yield},
    {"sleep",            "(J)V",       (void *)&JVM_Sleep},
    {"currentThread",    "()" THD,     (void *)&JVM_CurrentThread},
    {"countStackFrames", "()I",        (void *)&JVM_CountStackFrames},
    {"interrupt0",       "()V",        (void *)&JVM_Interrupt},
    {"isInterrupted",    "(Z)Z",       (void *)&JVM_IsInterrupted},
    {"holdsLock",        "(" OBJ ")Z", (void *)&JVM_HoldsLock},
    {"getThreads",        "()[" THD,   (void *)&JVM_GetAllThreads},
    {"dumpThreads",      "([" THD ")[[" STE, (void *)&JVM_DumpThreads},
    {"setNativeName",    "(" STR ")V", (void *)&JVM_SetNativeThreadName},
};

#undef THD
#undef OBJ
#undef STE
#undef STR JNIEXPORT void JNICALL Java_java_lang_Thread_registerNatives(JNIEnv *env, jclass cls) {
    (*env)->RegisterNatives(env, cls, methods, ARRAY_LENGTH(methods));
}
 Copy code 

Back to our start0 Method , At this point, we go to find JVM_StartThread The way is that he is /hotspot/src/share/vm/prims/jvm.cpp In this document :

JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
  JVMWrapper("JVM_StartThread");
  JavaThread *native_thread = NULL;

  // We cannot hold the Threads_lock when we throw an exception,
  // due to rank ordering issues. Example: we might need to grab the
  // Heap_lock while we construct the exception.
  bool throw_illegal_thread_state = false;

  // We must release the Threads_lock before we can post a jvmti event
  // in Thread::start.
  {
    // Ensure that the C++ Thread and OSThread structures aren't freed before
    // we operate.
    MutexLocker mu(Threads_lock);

    // 1.  Judge  Java  Whether the thread starts , If it's already started , Throw an exception 
    if (java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)) != NULL) {
      throw_illegal_thread_state = true;
    } else {
      // 2.  If not created , The thread will be created  
      jlong size =
             java_lang_Thread::stackSize(JNIHandles::resolve_non_null(jthread));  
      size_t sz = size > 0 ? (size_t) size : 0;
      //  Virtual machine creation  JavaThread,  This class will create operating system threads , And then connect  Java  Threads  
      native_thread = new JavaThread(&thread_entry, sz);

      if (native_thread->osthread() != NULL) {
        // Note: the current thread is not being used within "prepare".
        native_thread->prepare(jthread);
      }
    }
  }

  if (throw_illegal_thread_state) {
    THROW(vmSymbols::java_lang_IllegalThreadStateException());
  }

  assert(native_thread != NULL, "Starting null thread?");

  if (native_thread->osthread() == NULL) {
    // No one should hold a reference to the 'native_thread'.
    delete native_thread;
    if (JvmtiExport::should_post_resource_exhausted()) {
      JvmtiExport::post_resource_exhausted(
        JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS,
        "unable to create new native thread");
    }
    THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
              "unable to create new native thread");
  }

  //  Set the thread state to  Runnable
  Thread::start(native_thread);

JVM_END
 Copy code 

JavaThread Class, let's take a look at , He is through os::create_thread Function to create Java The corresponding kernel thread

JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) :
  Thread()
{
  if (TraceThreadEvents) {
    tty->print_cr("creating thread %p", this);
  }
  initialize();
  _jni_attach_state = _not_attaching_via_jni;
  set_entry_point(entry_point);
  os::ThreadType thr_type = os::java_thread;
  thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread :
                                                     os::java_thread;
  //  establish Java The kernel line corresponding to the thread 
  os::create_thread(this, thr_type, stack_sz);
  _safepoint_visible = false;
}

 Copy code 

os:create_thread In fact, it is mainly used to support cross platform thread creation , With Linux For example (hotspot/src/os/linux/vm/os_linux.cpp):

bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) {
  // ...
  
  //  establish  OSThread  Kernel thread objects 
  OSThread* osthread = new OSThread(NULL, NULL);
  //  binding 
  thread->set_osthread(osthread);

  pthread_t tid;
  // pthread_create  by  linux api  Used to create threads .
  int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);
  // ... 
  return true;
}
 Copy code 

We can go through ubantu Use the console to query the interface information

man pthread_create To query documents

image.png Through the documentation, we can understand , When pthread_create After the thread is created, the function will call the third parameter to pass the past callback function

int ret = pthread_create(&tid, &attr, (void* ()(void)) java_start, thread);

Here it is java_start function

// Thread start routine for all newly created threads
static void *java_start(Thread *thread) {

  //  Mainly called  Thread  Of  run  Method 
  thread->run();

  return 0;
}
 Copy code 

stay thread.cpp in JavaThread::run Method finally calls thread_main_inner Method :

// The first routine called by a new Java thread
void JavaThread::run() {
 

  // We call another function to do the rest so we are sure that the stack addresses used
  // from there will be lower than the stack base just computed
  thread_main_inner();

  // Note, thread is no longer valid at this point!
}
 Copy code 

stay thread_main_inner In the way , Create... Before calling us JavaThread Object is passed in entry_point Method :

void JavaThread::thread_main_inner() {

  if (!this->has_pending_exception() &&
      !java_lang_Thread::is_stillborn(this->threadObj())) {
    {
      ResourceMark rm(this);
      this->set_native_thread_name(this->get_thread_name());
    }
    HandleMark hm(this);
    //  call  entry_point  Method 
    this->entry_point()(this, this);
  }

  DTRACE_THREAD_PROBE(stop, this);

  this->exit(false);
  delete this;
}

 Copy code 

From the above code, we can see that we created a JavaThread object , Then it came in thread_entry Method

// JVM_StartThread  Create operating system threads , perform  thread_entry  function 
static void thread_entry(JavaThread* thread, TRAPS) {
  HandleMark hm(THREAD);
  Handle obj(THREAD, thread->threadObj());
  JavaValue result(T_VOID);
  // Thrad.start()  call  java.lang.Thread  Class  run  Method 
  JavaCalls::call_virtual(&result,
                          obj,
                          KlassHandle(THREAD, SystemDictionary::Thread_klass()),
                          vmSymbols::run_method_name(),
                          vmSymbols::void_method_signature(),
                          THREAD);
}
 Copy code 

Let's take a look at our Java in Thread Class run Method

public void run() {
    if (target != null) {
        // Thread.run()  Call again  Runnable.run()
        target.run(); 
    }
}
 Copy code 

Reference material

版权声明
本文为[Lao Zheng_]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/09/20210909124112705o.html

Scroll to Top