Due to the recent performance problems encountered in the work, try to use multithreading to achieve, the results are several times faster
The following is a multithreaded query part of the code, to provide you with reference:
Thread class:
Class with return value to implement the callable interface, the specific business logic is not implemented, just write an empty method in the inside
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;
}
/**
* Query method
*
* @param date Dates
* @param i I
*/
private void Querylistbymap (String date) {
Specific query
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 calls the thread class to implement multithreaded queries:
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 ();
Number of threads created
int count = len% 2 = = 0? LEN/2: (LEN/2) + 1;
Number of thread pools created
Executorservice pool = Executors.newfixedthreadpool (count);
list<callable<list<workload>>> tasks = new arraylist<callable<list<workload>>> ();
for (int i = 0; i < count; i++) {
Each thread is responsible for inserting the starting position of the data
int start = i * 2;
Each thread is responsible for inserting the end position of the data
int end = i = = count-1? I * 2 + (LEN-I * 2): i* 2;
map<string, object> parametersclone = new hashmap<string, object> ();
Parametersclone.putall (parameters);
Thread return value
callable<list<workload>> task = new Querythread (Parametersclone, SqlCode, dates, start, end, Workloadservice);
Tasks.add (Task);
}
Execution thread
List<future<list<workload>>> futures = Pool.invokeall (tasks);
Processing thread returns results
if (futures! = null && futures.size () > 0) {
for (future<list<workload>> future:futures) {
Workloads.addall (Future.get ());
}
}
Close the thread pool
Pool.shutdown ();
}
Long endTime = System.currenttimemillis ();
Logger.info ("Multiple cases Report query Total spent:" + (Endtime-starttime)/1000 + "seconds time!");
return workloads;
}
}
Follow up on multi-threaded batch save and Android multi-threaded support breakpoint continued download content
Because it is the actual project, so the specific content has been deleted, directly modified for your reference
Java multithreaded Query