多线程的使用
使用多线程创建文件10个线程负责100000个文件的创建(文件名1~100000.txt)
public class Test {
public static void main(String[] args) {
File file=new File("D://test/test3");
file.mkdir();
int i=0;
while(i<10){
build b=new build(file, 1+i*10000);
b.setName("线程"+i);
b.start();
i++;
}
}
}
class build extends Thread{
File f;
int i=0;
int j=0;
public build(File f, int i) {
super();
this.f = f;
this.i = i;
}
@Override
public void run() {
for( j=i;j<10000+i;j++){
synchronized (this) {
File newFile=new File(f,j+".txt");
try {
newFile.createNewFile();
System.out.println(Thread.currentThread().getName()+":"+j);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}