小凡156

博客园 首页 新随笔 联系 订阅 管理
 1     /**    
      使此线程开始执行;Java虚拟机调用此线程的<code>run</code>方法。
2 * Causes this thread to begin execution; the Java Virtual Machine 3 * calls the <code>run</code> method of this thread. 4 * <p>
      结果是两个线程同时运行:当前线程(从对<code>start</code>方法的调用返回)和另一个线程(执行其<code>run</code>方法)。
5 * The result is that two threads are running concurrently: the 6 * current thread (which returns from the call to the 7 * <code>start</code> method) and the other thread (which executes its 8 * <code>run</code> method). 9 * <p>

      多次启动线程是不合法的。      
10 * It is never legal to start a thread more than once.

      特别是,线程一旦完成执行就不能重新启动。
11 * In particular, a thread may not be restarted once it has completed 12 * execution. 13 *
      异常IllegalThreadStateException(如果线程已启动)。
14 * @exception IllegalThreadStateException if the thread was already 15 * started. 16 * @see #run() 17 * @see #stop() 18 */ 19 public synchronized void start() { 20 /** 21 * This method is not invoked for the main method thread or "system" 22 * group threads created/set up by the VM. Any new functionality added 23 * to this method in the future may have to also be added to the VM. 24 * 25 * A zero status value corresponds to state "NEW". 26 */

      零状态值对应于状态“NEW”。
27 if (threadStatus != 0) 28 throw new IllegalThreadStateException(); 29     
      通知组此线程即将启动,以便可以将其添加到组的线程列表中,并且可以减少组的未启动计数。 30 /* Notify the group that this thread is about to be started 31 * so that it can be added to the group's list of threads 32 * and the group's unstarted count can be decremented. */ 33 group.add(this); 34 35 boolean started = false; 36 try { 37 start0(); 38 started = true; 39 } finally { 40 try { 41 if (!started) { 42 group.threadStartFailed(this); 43 } 44 } catch (Throwable ignore) {
            什么都不做如果start0抛出了一个throwable,那么它将被传递到调用堆栈上
45 /* do nothing. If start0 threw a Throwable then 46 it will be passed up the call stack */ 47 } 48 } 49 } 50 51 private native void start0();

 start0调用jvm的JVM_StartThread函数

 1 JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
 2 
 3   JVMWrapper("JVM_StartThread");
 4   JavaThread *native_thread = NULL;
 5   bool throw_illegal_thread_state = false;
 6   {
 7     MutexLocker mu(Threads_lock);
 8 
 9     if (java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)) != NULL) {
10 
11       throw_illegal_thread_state = true;
12 
13     } else {
14 
15       jlong size =
16 
17              java_lang_Thread::stackSize(JNIHandles::resolve_non_null(jthread));
18 
19 
20       size_t sz = size > 0 ? (size_t) size : 0;
21       关注这里,创建新线程
22       native_thread = new JavaThread(&thread_entry, sz);
23 
24       if (native_thread->osthread() != NULL) {
25         native_thread->prepare(jthread);
26       }
27     }
28   }
29 
30 
31   if (throw_illegal_thread_state) {
32 
33     THROW(vmSymbols::java_lang_IllegalThreadStateException());
34 
35   }
36 
37   assert(native_thread != NULL, "Starting null thread?");
38 
39   if (native_thread->osthread() == NULL) {
40 
41     // No one should hold a reference to the 'native_thread'.
42 
43     delete native_thread;
44 
45     if (JvmtiExport::should_post_resource_exhausted()) {
46 
47       JvmtiExport::post_resource_exhausted(
48 
49         JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS,
50 
51         "unable to create new native thread");
52 
53     }
54 
55     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
56 
57               "unable to create new native thread");
58 
59   }
60 
61 
62   Thread::start(native_thread);
63 
64 
65 JVM_END
 1 static void thread_entry(JavaThread* thread, TRAPS) {
 2 
 3   HandleMark hm(THREAD);
 4 
 5   Handle obj(THREAD, thread->threadObj());
 6 
 7   JavaValue result(T_VOID);
 8

    调用run方法

 9   JavaCalls::call_virtual(&result,
10 
11                           obj,
12 
13                           KlassHandle(THREAD, SystemDictionary::Thread_klass()),
14 
15                           vmSymbols::run_method_name(),
16 
17                           vmSymbols::void_method_signature(),
18 
19                           THREAD);
20 
21 }

总结:

1:JVM通过new JavaThread(&thread_entry, sz); 创建一个系统内核线程。

2:在内核线程的初始运行方法中,利用JavaCalls模块,调用java线程的run()方法,开始java级别的线程执行。

posted on 2019-11-01 18:22  小凡156  阅读(827)  评论(0编辑  收藏  举报