是的,你可以使用多线程提高性能,而一个是制作磁盘I / O,另一个是做网络I / O,我会写一个小例子.
这里是一个例子,最好在睡觉之前阅读:)使用类ReadThenAll(5)的构造函数;您创建5个线程来浏览文件夹和子文件夹.玩得开心 !!
package foo;
import java.io.File;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class ReadThenAll {
// subfolders to explore
private final Queue exploreList = new ConcurrentLinkedQueue();
private long counter = 0;
public void count() {
counter++;
}
public static void main(String[] args) {
ReadThenAll me = new ReadThenAll(5);
me.scan("/tmp");
}
int[] threads;
public ReadThenAll(int numberOfThreads) {
threads = new int[numberOfThreads];
for (int i = 0; i < threads.length; i++) {
threads[i] = -1;
}