我正在尝试在线程中执行一些代码。我扩展了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
答案 0 :(得分:1)
此程序的问题在于,线程只是被创建而从未被破坏,因为您自己中断了正在运行的线程,这就是它被弄乱的地方。线程并没有停止,而是继续在循环内执行。我不会评论这是否是正确的方法。但我建议寻找其他替代方法。我在您的程序中做了如下修改:
public class ThreadTest {
static Publish publish;
public static void main(String[] args) {
System.out.println("Initial active threads : " + Thread.activeCount());
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) {
counter = 1;
publish = new Publish();
publish.start();
System.out.println("active threads : " + Thread.activeCount());
return;
}
}
}
}
}
这是程序的输出:
Initial active threads : 2
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
active threads : 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
active threads : 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
active threads : 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
active threads : 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
active threads : 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
active threads : 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
active threads : 4
counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5
active threads : 4
counter is: 1
counter is: 2
counter is: 3
如果要查看正在创建的线程总数,而又不想在程序中完全停止,请尝试为当前活动线程放置打印语句。
我希望它会有所帮助。干杯!