public class ThreadTest
extends Thread {
private static int threadCount = 0;
private int threadNum;
private int i = 5;
//线程的构造函数,每次执行线程计数器threadCount加一
public ThreadTest(int threadNo) {
threadNum = threadNo;
threadCount++;
System.out.println("创建线程" + threadNum);
}
public void run() {
while (true)
{
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("线程" + threadNum + ",计数:" + i);
if (--i == 0)
return;
}
}
public static void main(String[] args) {
//依次建立5个线程
for (int i = 0; i <= 5; i++)
{
new ThreadTest(i).start();
}
}
}