使用FastFoot轻松搞定JAVA的多任务处理
(一)FastFoot简介
FastFoot(快脚)是一款处理JAVA多任务的利器,可以非常简单和高效的进行多任务的分解并处理,并在全部任务处理后,对所有任务的结果及进行合并返回,使用者可以当做同步方法来调用,完全不用操作复杂的JAVA线程同时可以达到很高执行效率。
(二)下载FastFoot
你可以通过github来下载 ,这个工具。
(三)如何导入FastFoot
可以两种方式引用FastFoot:
- 引用jar包方式,在下载工程的
target中已经生成了FastFoot-1.0-SNAPSHOT.jar,可以引用。 - 直接使用java文件,拷贝在src\main\java\org\lwl\fastfoot目录的FastFoot.java文件到自己的工程即可。FastFoot实现并不复杂,目前只一个文件就搞定了所有的事情。
(四)FastFoot的调用方法
FastFoot目前提供两种调用方法,一种为默认方法自动启动20个线程进行多任务处理。另一种可以通过传递feet参数来控制启动线程的个数。
- 默认调用方法
/**
* Run Task by by default 20 threads,and wait all task completed to get all result。
*
* @param taskList task list
* @param function function for run every task
* @param <T> task class type
* @param <R> result class type
* @return task and result map
*/
public static <T, R> Map<T, R> runTasks(List<T> taskList, Function<T, R> function){
return runTasks(taskList, function, 20);
}
- 指定feet调用方法
/**
* Run Task by multi thread,and wait all task completed to get all result。
*
* @param taskList task list
* @param function function for run every task
* @param feet create thread number when run the tasks
* @param <T> task class type
* @param <R> result class type
* @return task and result map
*/
public static <T, R> Map<T, R> runTasks(List<T> taskList, Function<T, R> function, int feet)
官网上已经给出了相应的例子,下面简单翻译一下。
(五)官网例子
- 创建简单的多任务,实际情况可以是数据库,文件操作等
List<Integer> taskList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
- 创建函数来执行多任务,这里使用了java8的函数式编程,不会的需要学习一下写法。
Function<Integer, List<String>> function = count -> {
List<String> list = new ArrayList<>();
for (int i = 0; i < count; i++) {
try {
// Simulate time-consuming operations
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
list.add(String.valueOf(i+1));
}
return list;
};
- 如在不使用FastFoot时,以单线程方式进行处理如下
long start = System.currentTimeMillis();
Map<Integer, List<String>> map = new HashMap<>();
Map<Integer, List<String>> finalMap = map;
taskList.forEach(task -> {
finalMap.put(task, function.apply(task));
});
System.out.println("Not use FastFoot, all task complete by costing:" + (System.currentTimeMillis() - start)+"ms");
System.out.println("result:" + map);
- 使用 FastFoot,feet参数即线程数用5
start = System.currentTimeMillis();
map = FastFoot.runTasks(taskList, function, 5);
System.out.println("Use Fast Foot with 5 threads, all task complete by costing:" + (System.currentTimeMillis() - start)+"ms");
System.out.println("result:" + map);
- 使用 FastFoot,默认不传foots参数即线程数用20
start = System.currentTimeMillis();
map = FastFoot.runTasks(taskList, function);
System.out.println("Use Fast Foots with 20 threads, All task complete by costing:" + (System.currentTimeMillis() - start)+"ms");
System.out.println("result:" + map);
(六)执行结果
Not use FastFoot, all task complete by costing:22845ms
result:{1=[1], 2=[1, 2], 3=[1, 2, 3], 4=[1, 2, 3, 4], 5=[1, 2, 3, 4, 5], 6=[1, 2, 3, 4, 5, 6], 7=[1, 2, 3, 4, 5, 6, 7], 8=[1, 2, 3, 4, 5, 6, 7, 8], 9=[1, 2, 3, 4, 5, 6, 7, 8, 9], 10=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 11=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 12=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 13=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 14=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], 15=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 16=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 17=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], 18=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18], 19=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], 20=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]}
Use FastFoot with 5 threads, all task complete by costing:5135ms
result:{1=[1], 2=[1, 2], 3=[1, 2, 3], 4=[1, 2, 3, 4], 5=[1, 2, 3, 4, 5], 6=[1, 2, 3, 4, 5, 6], 7=[1, 2, 3, 4, 5, 6, 7], 8=[1, 2, 3, 4, 5, 6, 7, 8], 9=[1, 2, 3, 4, 5, 6, 7, 8, 9], 10=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 11=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 12=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 13=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 14=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], 15=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 16=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 17=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], 18=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18], 19=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], 20=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]}
Use FastFoot with 20 threads, All task complete by costing:2032ms
result:{1=[1], 2=[1, 2], 3=[1, 2, 3], 4=[1, 2, 3, 4], 5=[1, 2, 3, 4, 5], 6=[1, 2, 3, 4, 5, 6], 7=[1, 2, 3, 4, 5, 6, 7], 8=[1, 2, 3, 4, 5, 6, 7, 8], 9=[1, 2, 3, 4, 5, 6, 7, 8, 9], 10=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 11=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 12=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 13=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 14=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], 15=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 16=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 17=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], 18=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18], 19=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], 20=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]}
(七)结论
在使用FastFoot默认线程数20时执行速度最快,相当于单线程时的1/10。FastFoot最佳实践是设定线程数与任务数相等,这样执行的总耗时就几乎等于多任务中的最大单任务耗时。