gpt4 book ai didi

java - 错误 Java 异常 java.lang.IllegalThreadStateException

转载 作者:行者123 更新时间:2023-11-30 03:57:35 25 4
gpt4 key购买 nike

在 Java 中,我收到此异常:

线程“main”中的异常 java.lang.IllegalThreadStateException

这是代码:

class Worker extends Thread
{
public void run() {
System.out.println("#");
}
}

class Seconda extends Thread
{
public void run() {
System.out.println("*");
}
}


public class First
{
public static void main(String args[]) {
int contatore=0;
Worker prova = new Worker();
Seconda prova2 = new Seconda();
prova.start();
prova2.start();
for(;;) {
if(!prova.isAlive()) { prova.start();
contatore++;
}
if(!prova2.isAlive()) { prova2.start();
contatore++;
}
if(contatore==50) System.out.println("");
}

}
}

异常是什么意思?

最佳答案

您正在尝试启动一个已经启动的线程。

您需要做的是在 for 循环内创建 prova 和 prova2?

public static void main(String args[]) {
int contatore=0;
//Worker prova = new Worker();
//Seconda prova2 = new Seconda();
//prova.start();
//prova2.start();
for(;;) {
Worker prova = new Worker();
Seconda prova2 = new Seconda();
if(!prova.isAlive()) {
prova.start();
contatore++;
}
if(!prova2.isAlive()) {
prova2.start();
contatore++;
}
if(contatore==50) System.out.println("");
}
}

关于java - 错误 Java 异常 java.lang.IllegalThreadStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22759590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com