public class RunnableTest
implements Runnable {
public static void main(String[] args) {
for (int i = 0; i <= 5; i++) {
//使用Runnable接口的类,必须手工创建一个线程
new Thread(new RunnableTest(i)).start();
}
}
private static int threadCount = 0;
private int threadNum;
private int i = 5;
//Runnable的构造函数,每次执行线程计数器threadCount加一
public RunnableTest(int threadNo) {
threadNum = threadNo;
threadCount++;
System.out.println("创建线程" + threadNum);
}
public void run() {
while (true) {
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("线程" + threadNum + " ,计数" + i);
if (--i == 0)
return;
}
}
}