您的位置:首页 > 编程语言 > Java开发

java.lang.IllegalThreadStateException

2018-01-31 14:08 387 查看
这个问题的原因是:同一个Thread重复调用了start方法,如果你想开启多个线程,并且共享资源的时候,有两个方法
1.把共享资源设置为static
2.使用runnable

下面是一组上产上述错误原因的代码

public class Test1 {
public static void main(String[] args) {
Thread thread = new Demo();
thread.start();
thread.start();//这里删掉就不会报IllegalThreadStateException异常
}
}

class Demo extends Thread{
int x = 0;
@Override
public void run() {
while(true){
if(x>100){
System.out.println(x++);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Illegal Thread State