package com.test;
import java.io.File;
import junit.framework.TestCase;
public class TestThread extends TestCase {
protected void setUp() throws Exception {
super.setUp();
}
public void testThread() throws Exception {
for (int i = 0; i < 5; i++) {
new Thread(new MyThread(i + 1)).start();
}
}
public class MyThread implements Runnable {
int count = 1, number;
public MyThread(int num) {
number = num;
System.out.println("创建线程 " + number);
}
public void run() {
while (true) {
System.out.println("线程 " + number + ":计数 " + count);
try {
File file = new File("D:\\" + count + ".txt");
if (!file.exists()) {
file.createNewFile();
System.out.println("D:\\" + count + ".txt 创建成功");
}
} catch (Exception e) {
e.printStackTrace();
}
if (++count == 6)
return;
}
}
}
}
多线程小例子
最新推荐文章于 2021-09-20 22:18:17 发布