java start 多次调用_同一个线程多次调用start()会出现的问题

/*** Causes this thread to begin execution; the Java Virtual Machine

* calls the run method of this thread.

让这个线程开始执行。JVM会调用这个线程的run()方法。

*

* The result is that two threads are running concurrently: the

* current thread (which returns from the call to the

* start method) and the other thread (which executes its

* run method).

*

* It is never legal to start a thread more than once.一个线程多次调用start()方法是非法的。

* In particular, a thread may not be restarted once it has completed

* execution.

*特别说明:一个线程执行完后,不太可能重新运行。

*@exceptionIllegalThreadStateException if the thread was already

* started.

如果该线程已经启动,则再次调用start()方法,就会抛出IllegalThreadStateException异常。

*@see#run()

*@see#stop()*/

public synchronized voidstart() {/*** This method is not invoked for the main method thread or "system"

* group threads created/set up by the VM. Any new functionality added

* to this method in the future may have to also be added to the VM.

*

* A zero status value corresponds to state "NEW".*/

if (threadStatus != 0)//新的线程threadState值是0

throw newIllegalThreadStateException();/*Notify the group that this thread is about to be started

* so that it can be added to the group's list of threads

* and the group's unstarted count can be decremented.*/group.add(this);//通知线程组该线程将要开始运行,这样该线程就会被添加到线程列表中,此时列表的unstarted数将会减少。

boolean started = false;try{

start0();//调用原生方法态方法启动线程

started = true;//已经运行的标记设置为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*/}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值