File sources = new File(xmlFolderPath);
File[] files = sources.listFiles();
如上面的代码,我希望用多线程处理files里面的每一个file,因为每一个file要处理的东西挺多,如果用单线程的话要花很长的时间,希望各位大侠能够提供些宝贵意见,谢谢!!Java多线程数组
File[] files = sources.listFiles();
如上面的代码,我希望用多线程处理files里面的每一个file,因为每一个file要处理的东西挺多,如果用单线程的话要花很长的时间,希望各位大侠能够提供些宝贵意见,谢谢!!Java多线程数组
// files文件处理; } public static void main(String[] args) { Thread thread = new Thread(new Test());
thread.start();
}
}
每一个file的处理就是一个任务,然后将这些任务交个线程池来处理就可以了。
import java.io.File;
import java.io.FileFilter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;/**
* file handler with multi-thread, only the simple demo
*/
public class FilesThread { private static final int THREAD_CORE_SIZE = 10;
private static final int THREAD_MAX_SIZE = 10;
private static final int KEEP_ALIVE_TIME = 20000;
private static final AtomicInteger threadIndex = new AtomicInteger();
private static final String FILE_PATH = "c:\\windows\\system32\\"; private static final ThreadFactory tf = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
final Thread t = new Thread(r);
t.setDaemon(false);
t.setName("fileThreadHandler" + threadIndex.incrementAndGet());
return t;
}
}; public static void main(String[] args) {
Collection<Callable<AsyncResult>> workers = new ArrayList<Callable<AsyncResult>>();
final File f = new File(FILE_PATH);
final File[] filteredFiles = f.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return true;
}
}); for (final File file : filteredFiles) {
workers.add(new FileWorkerHandler(file));
} if (workers.isEmpty()) {
return;
} ThreadPoolExecutor pool = null;
try {
pool = new ThreadPoolExecutor(THREAD_CORE_SIZE, THREAD_MAX_SIZE,
KEEP_ALIVE_TIME, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(10), tf,
new ThreadPoolExecutor.CallerRunsPolicy());
pool.invokeAll(workers);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (pool != null) {
pool.shutdown();
}
}
} /**
* the file worker implements Callable
*/
static class FileWorkerHandler implements Callable<AsyncResult> {
private final File file; public FileWorkerHandler(final File f) {
this.file = f;
} @Override
public AsyncResult call() throws Exception {
final Thread t = Thread.currentThread();
if (t.isInterrupted()) {
return null;
} // handle file logic
if (file.isDirectory()) {
return null;
} System.out.println("the current thread name:" + t.getName()
+ " and the file name is :" + file.getName()); // do something and return the result that you want
final Object result = new Object();
return new AsyncResult(result);
}
} /**
* the result that will be returned
*/
static class AsyncResult implements Serializable {
private static final long serialVersionUID = 5684133216059639260L;
private Object object; public Object getObject() {
return object;
} public AsyncResult(final Object o) {
this.object = o;
} }
}
public class Test implements Runnable {
File file;
public Test(File file){
this.file=file;
} public void run() {
// this.file 就是你要处理的其中一个文件
}
public static void main(String[] args) {
File sources = new File(xmlFolderPath);
File[] files = sources.listFiles();
for(File file:files){ //循环的代码有可能不对,因为很久没有写java代码了,这个循环是Python的,自己修改。
Thread thread = new Thread(new Test(file));
thread.start();
}
}
}