可配置线程数量
@Component
public class DemoDataThreadPool {
@Value("thread.config.count")
private String configCount;
private ExecutorService executorService;
public ExecutorService getExecutorService(){
if (executorService == null){
//使用线程池进行任务
int threadCount = Integer.valueOf(configCount);
final long startTime = System.currentTimeMillis();
if (executorService == null) {
ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setNameFormat("Demo-%d")
.setDaemon(true)
.build();
final long middleTime = System.currentTimeMillis();
executorService = Executors.newFixedThreadPool(threadCount, threadFactory);
}
final long endTime = System.currentTimeMillis();
return executorService;
}
return executorService;
}
}
引用线程池:
@Service
public class DemoDataAction {
@Autowired
DemoDataThreadPool demoDataThreadPool;
@Autowired
DemoService demoService;
public void syncDemoData(){
// int pageSize = 1000;
// List limitList = Lists.newArrayList();
//
// Integer pageNum = (limitList.size() % pageSize == 0) ? limitList.size()/pageSize : limitList.size()/pageSize + 1;
// for (int i = 0; i < pageNum; i++) {
// List resultList = demoService.queryBatchTask();
// if (resultList == null || resultList.size() < 0) {
// return;
// }
// allList.addAll(resultList);
// }
List allList = demoService.queryBatchTask();
CountDownLatch countDownLatch = new CountDownLatch(allList.size());
List<Map<String, Object>> mapList = Lists.newArrayList();
for (int i = 0; i < allList.size(); i++) {
BatchTaskOperation taskOperation = new BatchTaskOperation(demoService, mapList, countDownLatch);
demoDataThreadPool.getExecutorService().execute(taskOperation);
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
//log
}
}
}
执行线程任务
@Log4j
@Component
public class BatchTaskOperation implements Runnable{
private DemoService demoService;
private List<Map<String, Object>> mapList;
private CountDownLatch countDownLatch;
public BatchTaskOperation(DemoService demoService, List<Map<String, Object>> mapList, CountDownLatch countDownLatch){
this.demoService = demoService;
this.mapList = mapList;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
this.operationHandler();
}
public void operationHandler(){
try {
final String threadName = Thread.currentThread().getName();
//TODO
System.out.println("threadName:" + threadName);
}catch (Exception e){
//log
}finally {
countDownLatch.countDown();
}
}
}
自定义线程池:
@Log4j
public class DemoTaskMain {
private static Logger logger = LoggerFactory.getLogger(DemoTaskMain.class);
private static String CLASS_NAME = "DemoTaskMain";
private static final int CORE_POOL_SIZE = 5;
private static final int MAX_POOL_SIZE = 8;
private static ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("process-task-thread-%d").build();
private static BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(100);
private static ExecutorService executorService = new ThreadPoolExecutor(
CORE_POOL_SIZE,
MAX_POOL_SIZE,
60L,
TimeUnit.SECONDS,
workQueue,
threadFactory,
//调用者执行策略(CallerRunsPolicy:既不抛异常,也不丢任务,让主线程执行)
new ThreadPoolExecutor.CallerRunsPolicy());
public void handlerTaskMain(){
logger.info(CLASS_NAME, "handlerTaskMain", "当前时间:"+DateUtiles.formatDate(new Date()));
//清除工作队列中未完成的任务
workQueue.clear();
//所有任务类型
String[] taskTypes = {"1","2","3"};
//所有任务类型
for (String taskType : taskTypes) {
if (StringUtils.isEmpty(taskType)){
continue;
}
logger.info(CLASS_NAME, "handlerTaskMain", "任务类型:"+taskType);
HashMap<Object, Object> paramsMap = Maps.newHashMap();
List<Map<String, Object>> batchTaskList = taskSerice.getDemoTaskBatch();
batchTaskList = batchTaskList != null ? batchTaskList : Collections.EMPTY_LIST;
logger.info(CLASS_NAME, "handlerTaskMain", "任务数据量:"+ batchTaskList.size());
//主线程将各个批次的任务封装到Callable,然后提交到队列
for (Map<String, Object> taskMap : batchTaskList) {
executorService.submit(new DemoCallableTask(taskMap));
}
}
}
/**
* 内部类的实现方式更加优雅:无需返回值
* 如果执行中出现异常,会在该异步线程中打印错误日志
*/
private class DemoCallableTask implements Callable<Void> {
private Map<String, Object> batchTaskMap;
public DemoCallableTask(Map<String, Object> map){
this.batchTaskMap = map;
}
@Override
public Void call() throws Exception {
//获取参数
String idBatch = (String) batchTaskMap.get("idBatch");
String threadName = Thread.currentThread().getName();
//线程标识
String seqhread = UUID.randomUUID().toString().replaceAll("-", "");
//TODO
return null;
}
}
}