由于最近工作遇到性能问题,尝试研究用多线程来实现,结果速度快了好几倍
下面是多线程查询的部分代码,提供给大家参考下:
线程类:
带返回值的类要实现Callable接口,具体业务逻辑没有实现,只是写了个空方法在里面
package com.sanfy.demo.thread;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import com.sanfy.demo.model.WorkLoad;
public class QueryThread implements Callable<List<WorkLoad>> {
private List<WorkLoad> workLoads;
private WorkLoadService workLoadService;
private Map<String, Object> parameters;
private List<String> dates;
private int begin;
private int end;
public QueryThread(Map<String, Object> parameters,List<String> dates, int begin, int end,WorkLoadService workLoadService) {
super();
this.parameters = parameters;
this.dates = dates;
this.begin = begin;
this.end = end;
this.workLoadService=workLoadService;
}
/**
* @return Map<Integer, EchartsTenInDatasDTO>
* @see java.lang.Callable#call()
*/
@Override
public Map<Integer, WorkLoad> call() {
for (int i = begin; i < end; i++) {
queryListByMap(dates.get(i));
}
return workLoads;
}
/**
* 查询方法
*
* @param date 日期
* @param i i
*/
private void queryListByMap(String date) {
//具体的查询
workLoads=workLoadService.queryList(parameters,date);
}
public List<WorkLoad> getWorkLoads() {
return workLoads;
}
public void setWorkLoads(List<WorkLoad> workLoads) {
this.workLoads = workLoads;
}
public WorkLoadService getWorkLoadService() {
return workLoadService;
}
public void setWorkLoadService(WorkLoadService workLoadService) {
this.workLoadService = workLoadService;
}
public Map<String, Object> getParameters() {
return parameters;
}
public void setParameters(Map<String, Object> parameters) {
this.parameters = parameters;
}
public List<String> getDates() {
return dates;
}
public void setDates(List<String> dates) {
this.dates = dates;
}
public int getBegin() {
return begin;
}
public void setBegin(int begin) {
this.begin = begin;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
}
serive调用线程类来实现多线程查询:
package com.sanfy.demo.thread;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.sanfy.demo.model.WorkLoad;
public class QuerySerivce {
private WorkLoadService workLoadService;
public List<WorkLoad> queryListsByMap(String sqlCode, Map<String, Object> parameters) throws Exception {
List<WorkLoad> workLoads=new ArrayList<WorkLoad>();
long startTime = System.currentTimeMillis();
List<String> dates = DateUtil.getCurrentInYear(parameters.get("startDate").toString(), 12);
if (dates != null && dates.size() > 0) {
int len = dates.size();
// 创建线程的个数
int count = len % 2 == 0 ? len / 2: (len / 2) + 1;
// 创建线程池数量
ExecutorService pool = Executors.newFixedThreadPool(count);
List<Callable<List<WorkLoad>>> tasks = new ArrayList<Callable<List<WorkLoad>>>();
for (int i = 0; i < count; i++) {
// 每个线程负责插入数据的开始位置
int start = i * 2;
// 每个线程负责插入数据的结束位置
int end = i == count - 1 ? i * 2 + (len - i * 2) : i* 2+ 2;
Map<String, Object> parametersClone = new HashMap<String, Object>();
parametersClone.putAll(parameters);
// 线程返回值
Callable<List<WorkLoad>> task = new QueryThread(parametersClone, sqlCode, dates, start, end,workLoadService);
tasks.add(task);
}
// 执行线程
List<Future<List<WorkLoad>>> futures = pool.invokeAll(tasks);
// 处理线程返回结果
if (futures != null && futures.size() > 0) {
for (Future<List<WorkLoad>> future : futures) {
workLoads.addAll(future.get());
}
}
// 关闭线程池
pool.shutdown();
}
long endTime = System.currentTimeMillis();
logger.info("多个情况报表查询总共花了:" + (endTime - startTime) / 1000 + "秒时间!");
return workLoads;
}
}
后续会补上多线程批量保存与android多线程支持断点续传下载的内容
由于是实际项目,所以把具体的内容删除了,直接修改的,供大家参考下