package com.javaconcurrencyprogramming.chapter1;

import java.util.concurrent.TimeUnit;

/**
* @description: 一个线程不能重复启动
* @author:
* @create:
**/

public class RestartThreadError {

public static void main(String[] args) {

Thread thread = new Thread(){

public void run(){
try {
TimeUnit.SECONDS.sleep(10);

}catch (InterruptedException e){
e.printStackTrace();
}
}
};

thread.start();//启动线程

thread.start();//重启线程报IllegalThreadStateException异常
}


}



//重新激活一个线程报IllegalThreadStateException

package com.javaconcurrencyprogramming.chapter1;

import java.util.concurrent.TimeUnit;

/**
* @description: 一个线程不能重复启动
* @author:
* @create:
**/

public class RestartThreadError {

public static void main(String[] args) throws InterruptedException {

Thread thread = new Thread(){

public void run(){
try {
TimeUnit.SECONDS.sleep(1);

}catch (InterruptedException e){
e.printStackTrace();
}
}
};

thread.start();

TimeUnit.SECONDS.sleep(4);

thread.start();//重启线程报IllegalThreadStateException异常
}


}

相关文章:

  • 2021-12-23
  • 2022-02-01
  • 2021-06-27
  • 2022-12-23
  • 2021-06-16
  • 2022-12-23
  • 2022-01-02
  • 2022-01-04
猜你喜欢
  • 2021-10-22
  • 2021-09-12
  • 2021-09-28
  • 2021-06-26
  • 2021-07-17
  • 2021-11-26
  • 2021-08-09
相关资源
相似解决方案