我正在尝试在一个线程中执行一些代码。我已经扩展了Thread类并将一些代码放在那里执行。我还通过catch块抛出异常来终止当前线程并重新启动它。这样做是可以的,但当新线程开始时,显示异常结果。
假设我的要求是打印计数器,例如 1,2,3,4,5
。第一次运行很好。但是当我启动新的实例时,结果是随机的,例如 1,1,1,1,1,1,12,4,6,1,1,1,1
。这是我的代码:
public class ThreadTest {
static Publish publish;
public static void main(String[] args) {
publish = new Publish();
publish.start();
}
static class Publish extends Thread {
static int counter = 1;
public void run(){
while (true) {
System.out.println("counter is: " + counter);
counter++;
try {
Thread.sleep(2000);
if (counter > 5) {
throw new InterruptedException("Exception thrown manually to restart thread");
}
} catch (Exception ex) {
publish.interrupt();
publish.getThreadGroup().interrupt();
counter = 1;
publish = new Publish();
publish.start();
}
}
}
}
}
以下是结果:
run:
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 2
counter is: 1
counter is: 3
counter is: 4
counter is: 1
counter is: 3
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 8
counter is: 8
counter is: 9
counter is: 11
counter is: 12
counter is: 12
counter is: 14
counter is: 15
counter is: 15
counter is: 17
counter is: 18
counter is: 19
counter is: 20
counter is: 1
counter is: 20
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 20
counter is: 1
counter is: 1
counter is: 20
counter is: 20
counter is: 20
counter is: 20
counter is: 20
counter is: 2
counter is: 20
counter is: 20
counter is: 2
counter is: 20
counter is: 1
counter is: 1
counter is: 2
counter is: 2
counter is: 2
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 4
counter is: 2
counter is: 4
counter is: 4
counter is: 2
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 2
counter is: 2
counter is: 2
counter is: 3
counter is: 4
counter is: 6
counter is: 1
counter is: 1
counter is: 1
counter is: 3
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 2
counter is: 5
counter is: 3
counter is: 4
counter is: 4
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 6
counter is: 7
counter is: 7
counter is: 2
counter is: 1
counter is: 1
counter is: 3
counter is: 3
counter is: 4
counter is: 6
counter is: 7
counter is: 7
counter is: 6
counter is: 9
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 3
counter is: 1
counter is: 1
counter is: 3
counter is: 2
counter is: 3
counter is: 3
counter is: 4
counter is: 6
counter is: 2
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 2
counter is: 4
counter is: 2
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 2
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 1
counter is: 2
counter is: 1
counter is: 4
counter is: 5
counter is: 5
counter is: 1
counter is: 2
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 3
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 4
counter is: 6
counter is: 7
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 8
counter is: 8
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 2
counter is: 3
counter is: 4
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 5
counter is: 7
counter is: 7
counter is: 1
counter is: 1
counter is: 1
counter is: 4
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 1
counter is: 3
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 2
counter is: 3
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 6
counter is: 8
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 5
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 3
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 5
counter is: 7
counter is: 8
counter is: 9
counter is: 10
counter is: 11
counter is: 12
counter is: 13
counter is: 1
counter is: 2
counter is: 3
counter is: 3
counter is: 5
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 2
counter is: 2
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 8
counter is: 2
counter is: 2
counter is: 2
counter is: 5
counter is: 6
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 4
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 4
counter is: 5
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 6
counter is: 8
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 8
counter is: 8
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 1
counter is: 3
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 3
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 5
counter is: 6
counter is: 7
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 8
counter is: 1
counter is: 2
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 2
counter is: 3
counter is: 1
counter is: 2
counter is: 2
counter is: 4
counter is: 1
counter is: 1
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 8
counter is: 9
counter is: 9
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 1
counter is: 1
counter is: 2
Exception in thread "Thread-260" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
counter is: 1
counter is: 2
at threadtest.ThreadTest$Publish.run(ThreadTest.java:49)
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 2
counter is: 3
counter is: 5
counter is: 6
counter is: 7
counter is: 1
counter is: 1
counter is: 3
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 3
counter is: 3
counter is: 6
counter is: 7
counter is: 8
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 6
counter is: 7
counter is: 8
counter is: 1
counter is: 3
Exception in thread "Thread-236" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
counter is: 1
at threadtest.ThreadTest$Publish.run(ThreadTest.java:49)
counter is: 1
counter is: 2
counter is: 1
counter is: 4
counter is: 5
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 3
counter is: 1
counter is: 1
counter is: 1
Exception in thread "Thread-165" java.lang.IllegalThreadStateException
counter is: 3
at java.lang.Thread.start(Thread.java:708)
at threadtest.ThreadTest$Publish.run(ThreadTest.java:49)
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 2
counter is: 3
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 6
counter is: 8
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 5
counter is: 7
counter is: 8
counter is: 9
counter is: 10
counter is: 11
counter is: 12
counter is: 13
counter is: 14
counter is: 15
counter is: 16
counter is: 17
counter is: 18
counter is: 19
counter is: 20
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 4
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 1
Exception in thread "Thread-156" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
at threadtest.ThreadTest$Publish.run(ThreadTest.java:49)
counter is: 1
counter is: 1
counter is: 1
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 8
counter is: 9
counter is: 10
counter is: 11
counter is: 12
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 2
counter is: 2
counter is: 3
counter is: 6
counter is: 7
counter is: 8
counter is: 2
counter is: 3
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 8
counter is: 1
counter is: 2
counter is: 2
counter is: 4
counter is: 5
counter is: 6
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 4
counter is: 6
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 3
counter is: 5
counter is: 5
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 8
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 1
counter is: 5
counter is: 6
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 8
counter is: 8
counter is: 10
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 4
counter is: 6
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 4
counter is: 6
counter is: 7
counter is: 8
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 8
counter is: 9
counter is: 10
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 7
counter is: 9
counter is: 9
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 8
counter is: 1
counter is: 2
counter is: 2
counter is: 4
counter is: 5
counter is: 5
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 3
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
counter is: 6
counter is: 7
counter is: 8
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 1
counter is: 2
counter is: 1
counter is: 2
counter is: 6
Runnable
或Callable
中,并进行替换。 - Thomasreturn
。 - markspace