Callable 或Runable实现类自定义内部类也可以;
//ExecutorService service = Executors.newFixedThreadPool(8);
ExecutorService service = Executors.newCachedThreadPool();
Future<String> result1 = service.submit(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(3000);
return "s1";
}
});
Future<String> result2 = service.submit(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(3000);
return "s2";
}
});
Future<String> result3 = service.submit(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(3000);
return "s3";
}
});
System.out.println(result1.get());
System.out.println(result2.get());
System.out.println(result3.get());
//service.shutdown();
或者下面内部类:
package com.realize.market.value.service;
import com.realize.market.value.domain.vos.DownloadReportSearchVo;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
/**
* 内部多线程任务类-查询所有模块echart 图形数据和文字描述
*/
public class MyEchartDataQueryTaskCallable implements Callable<Map> {
private Integer taskNum;
private Integer userId;
private String secCode;
private DownloadReportSearchVo downloadReportSearchVo;
private IDownloadReportService downloadReportService;
public MyEchartDataQueryTaskCallable(Integer taskNum, Integer userId, String secCode, DownloadReportSearchVo downloadReportSearchVo, IDownloadReportService downloadReportService) {
this.taskNum = taskNum;
this.userId = userId;
this.secCode = secCode;
this.downloadReportSearchVo = downloadReportSearchVo;
this.downloadReportService = downloadReportService;
}
@Override
public Map call() throws Exception {
System.out.println(">>>" + taskNum + "任务启动");
Date dateTmp1 = new Date();
//多线程处理耗时较久的查询各个模块方法;
Map<String, Object> wordInitDataMaps = new HashMap<>();
switch (taskNum) {
case 1://1-1 诊断结果 > 市值表现
wordInitDataMaps = downloadReportService.exportToWord_11(userId, secCode);
break;
case 2://1-2 诊断结果 > 流动性情况
wordInitDataMaps = downloadReportService.exportToWord_12(userId, secCode);
break;
case 3://1-3 诊断结果 > 股东结构分析
wordInitDataMaps = downloadReportService.exportToWord_13(userId, secCode);
break;
case 4://2-1 市值表现 > 市值对比
wordInitDataMaps = downloadReportService.exportToWord_21(userId, secCode, downloadReportSearchVo);
break;
case 5://2-2 市值表现 > 估值对比
wordInitDataMaps = downloadReportService.exportToWord_22(userId, secCode, downloadReportSearchVo);
break;
case 6://2-3 市值表现 > 股价对比
wordInitDataMaps = downloadReportService.exportToWord_23(userId, secCode, downloadReportSearchVo);
break;
case 7://3-1 流动性表现 > 换手率
wordInitDataMaps = downloadReportService.exportToWord_31(userId, secCode, downloadReportSearchVo);
break;
case 8://3-2 流动性表现 > 流动比
wordInitDataMaps = downloadReportService.exportToWord_32(userId, secCode, downloadReportSearchVo);
break;
case 9://4-1 股东结构统计 > 股东户数统计
wordInitDataMaps = downloadReportService.exportToWord_41(userId, secCode, downloadReportSearchVo);
break;
case 10://4-2 股东结构统计 > 户均持股市值
wordInitDataMaps = downloadReportService.exportToWord_42(userId, secCode, downloadReportSearchVo);
break;
case 11://4-3 股东结构统计 > 前10名股东持股情况
wordInitDataMaps = downloadReportService.exportToWord_43(userId, secCode, downloadReportSearchVo);
break;
case 12://4-4 股东结构统计 > 机构投资者情况
wordInitDataMaps = downloadReportService.exportToWord_44(userId, secCode, downloadReportSearchVo);
break;
case 13://2-1 市值表现 > 市值涨跌幅
wordInitDataMaps = downloadReportService.exportToWord_24(userId, secCode, downloadReportSearchVo);
break;
case 14://2-1 市值表现 > 市场市值对比
wordInitDataMaps = downloadReportService.exportToWord_25();
break;
}
Date dateTmp2 = new Date();
long time = dateTmp2.getTime() - dateTmp1.getTime();
System.out.println(taskNum + "任务终止" + ">>>单个任务耗时:" + time);
return wordInitDataMaps;
}
}