博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Thread 中的start()
阅读量:6826 次
发布时间:2019-06-26

本文共 2506 字,大约阅读时间需要 8 分钟。

1 /** 2      * Causes this thread to begin execution; the Java Virtual Machine 3      * calls the run method of this thread. 4      * 

5 * The result is that two threads are running concurrently: the 6 * current thread (which returns from the call to the 7 * start method) and the other thread (which executes its 8 * run method). 9 *

10 * It is never legal to start a thread more than once.11 * In particular, a thread may not be restarted once it has completed12 * execution.13 *14 * @exception IllegalThreadStateException if the thread was already15 * 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 added23 * 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 */27 if (threadStatus != 0)28 throw new IllegalThreadStateException();29 30 /* Notify the group that this thread is about to be started31 * so that it can be added to the group's list of threads32 * 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) {45 /* do nothing. If start0 threw a Throwable then46 it will be passed up the call stack */47 }48 }49 }50 51 private native void start0();

从jdk的官方文档可以看出一些关于start方法的一些信息:

  1.当一个thread被new出来后,线程的内部属性threadStatus==0;

  2.new thread之后,都会重写run方法实现业务,纵观start方法中,只包含了一个native方法,从文档中看出   

        /* Causes this thread to begin execution; the Java Virtual Machine calls the <code>run</code> method of this thread   */ 猜测推断出,native方法statr0调用了重写的run方法。

  3.当一个线程被start之后,再次start的时候,会抛出 IllegalThreadStateException 异常。

  4.  /*  In particular, a thread may not be restarted once it has complete execution. */   当一个线程结束他的生命周期之后,是不能再次调用start的。

  5.当线程start之后,会被加入到一个threadGroup中,最后会根据started标识位,判断是否移除此线程。

   

 

转载于:https://www.cnblogs.com/fengyue0520/p/10421828.html

你可能感兴趣的文章
spring集成 JedisCluster 连接 redis3.0 集群
查看>>
DOM基础2
查看>>
什刹海记忆
查看>>
硬盘分区时GPT和MBR的区别/选择
查看>>
Nginx 配置简述
查看>>
css后代选择器(div.class中间不带空格)
查看>>
在CentOS下利用Python+selenium获取腾讯首页的今日话题。
查看>>
Chapter 2 Open Book——38
查看>>
C++11特性(模板类 initializer_list)
查看>>
将数字转换千分位分隔形式
查看>>
父类和子类的构造方法的调用顺序
查看>>
2017第一周日
查看>>
git submodule的使用
查看>>
Python爬虫学习——获取网页
查看>>
javaWeb服务器配置
查看>>
linux 最常用的yum源remi
查看>>
[Go] Http / Net 相关资料
查看>>
Apple Pay在美超越PayPal
查看>>
word break相关问题的解法
查看>>
java中Scanner的nextLine()和next()的区别
查看>>