题记 : 最近再看公司的代码中,看到有使用 FutureTask的操作.于是就自己看了一下并且做一个简单的记录。
FutureTask来创建多线程任务,并且获取任务的结果
Callable: 这是一个接口类,并且只提供了一个call方法.
MyTask类 :
package com.yang.threadspringcase.task;
/*
*@author:BaoYang
*@Date: 2020/2/24
*@description:
*/
import java.util.concurrent.Callable;
public class MyTask implements Callable<Object>
{
private String param1;
private String param2;
public MyTask(String param1, String param2)
{
this.param1 = param1;
this.param2 = param2;
}
@Override
public Object call() throws Exception
{
for(int i=0;i<100;i++){
System.out.println(param1 + param2 + i);
}
try{
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
return true;
}
}
MyTaskMain类
package com.yang.threadspringcase.task;
/*
*@author:BaoYang
*@Date: 2020/2/24
*@description:
*/
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
public class MyTaskMain
{
public static void main(String[] args)
{
MyTask myTask = new MyTask("Gavin","Yang");
// 异步回调等待结果
FutureTask<Object> futureTask = new FutureTask<>(myTask);
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
cachedThreadPool.submit(futureTask);
cachedThreadPool.shutdown();
// Thread thread = new Thread(futureTask);
// thread.start();
try {
// 如果 futureTask没有执行完的话,就是需要等待结果
System.out.println("等待之前的处理哈哈哈h");
boolean result = (boolean) futureTask.get();
System.out.println("返回的Result结果是 ---> " + result);
}catch (Exception e){
e.printStackTrace();
}
}
}
MyTaskManyMain类:
package com.yang.threadspringcase.task;
/*
*@author:BaoYang
*@Date: 2020/2/24
*@description:
*/
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
public class MyTaskManyMain
{
public static void main(String[] args)
{
long startTime = System.currentTimeMillis();
List<FutureTask<Object>> futureTasks = new ArrayList<>();
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
for(Integer i=0;i<10;i++){
MyTask myTask = new MyTask(i.toString(),i.toString());
futureTasks.add(new FutureTask<>(myTask));
// cachedThreadPool.submit(new FutureTask<>(myTask));
}
for(FutureTask<Object> futureTask:futureTasks){
cachedThreadPool.submit(futureTask);
}
cachedThreadPool.shutdown();
for(Integer i=0;i<10;i++){
try{
boolean reuslt = (boolean) futureTasks.get(0).get();
System.out.println("The " + i + "个执行的结果" + reuslt);
}catch (Exception e){
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
System.out.println("消耗的时间是 ---> " + (endTime - startTime));
}
}
在MyTask中的call方法中,可以看到,在循环完了后,有进行休眠等待(模拟业务需要的耗时操作功能)
在 MyTaskMain中可以清楚的看到,
返回的Result结果是 --->打印这句话是在Thread.sleep的时间后打印出来出来的,说明当线程执行到get就进行等待来获取结果(这里目前可以理解为程序在这里进行了阻塞)。
在 MyTaskManyMain 中可以看到,多个线程同时执行,整个执行结果的耗时是只需要一个线程的耗时(这里就可以看出多线程的优点,在处理方面和耗时上可以看出来)
然后看到线程池这块,有交给Spring来进行管理的.
package com.yang.threadspringcase.config;
/*
*@author:BaoYang
*@Date: 2020/2/24
*@description:
* Main : Spring启动时自动加载一个ExecutorService对象.
*
*/
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Configuration
public class ThreadPoolConfig
{
@Bean
public ExecutorService getThreadPool(){
return Executors.newCachedThreadPool();
}
}
然后用到的时候,从Spring中获取就可以.