package thread;
import java.io.File;
public class Test_basic {
static String path = "xxxxxxx";
volatile static int result = 0;
static final String key = "class";
public static void main(String[] args) {
long begin = System.currentTimeMillis();
exec(new File(path));
System.out.println(result);
System.out.println((System.currentTimeMillis() -begin)+"mm");
// 2767
// 806mm
}
static void exec(File file) {
System.out.println(file.getAbsolutePath());
if (file.isFile()) {
result += Utils.getCount(file, key);
} else {
for (File item : file.listFiles()) {
exec(item);
}
}
}
}
package thread;
import java.io.File;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class Test_threadPool {
static String path = "xxxxxxx";
volatile static int result = 0;
static final String key = "class";
static ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
public static void main(String[] args) {
long begin = System.currentTimeMillis();
pool.submit(new ChildThread(path));
while (pool.getActiveCount() != 0) {
Thread.yield();
}
pool.shutdown();
System.out.println(result);
System.out.println(System.currentTimeMillis() - begin + "mm");
// 2767
// 562mm
}
synchronized static void addCount(int num) {
result = result + num;
}
static class ChildThread implements Runnable {
private String path;
public ChildThread() {
super();
}
public ChildThread(String path) {
super();
this.path = path;
}
@Override
public void run() {
System.out.println(path);
File file = new File(path);
if (file.isFile()) {
int count = Utils.getCount(file, key);
addCount(count);
} else {
for (File item : file.listFiles()) {
pool.submit(new ChildThread(item.getAbsolutePath()));
}
}
}
}
}
package thread;
import java.io.File;
import java.util.stream.Stream;
public class Test_lambda {
static String path = "xxxxxxx";
volatile static int result = 0;
static final String key = "class";
public static void main(String[] args) {
long begin = System.currentTimeMillis();
exec(new File(path));
System.out.println(result);
System.out.println((System.currentTimeMillis() - begin) + "mm");
// 2767
// 567mm
}
static void exec(File file) {
System.out.println(file.getAbsolutePath());
if (file.isFile()){
int count = Utils.getCount(file, key);
addCount(count);
} else {
Stream.of(file.listFiles()).parallel().forEach(i -> {
exec(i);
});
}
}
synchronized static void addCount(int num) {
result = result + num;
}
}
package thread;
import java.io.File;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
public class Test_forkJoin {
static String path = "xxxxxxx";
volatile static int result = 0;
static final String key = "class";
static ForkJoinPool pool = new ForkJoinPool(10);
public static void main(String[] args) {
long begin = System.currentTimeMillis();
exec(new File(path));
while(pool.getActiveThreadCount()!=0){
Thread.yield();
}
System.out.println(result);
System.out.println((System.currentTimeMillis() - begin) + "mm");
// 2767
// 601mm
}
static void exec(File file) {
pool.execute(new MyThread(file, key));
}
synchronized static void addCount(int num) {
result = result + num;
}
static class MyThread extends RecursiveAction {
/**
*
*/
private static final long serialVersionUID = 4794478361974081108L;
File file;
String key;
public MyThread(File file, String key) {
this.file = file;
this.key = key;
}
@Override
protected void compute() {
System.out.println(file.getAbsolutePath());
if (file.isFile()) {
int count = Utils.getCount(file, key);
addCount(count);
} else {
for (File item : file.listFiles()) {
new MyThread(item, key).fork();
}
}
}
}
}