多线程代码如下:
/**多线程处理ArrayList<SsqIssue> AL
* AL.get(i).getRedBalls()与 issuesAll.get(j).getRedBalls()比较,有a_numlimit个数据相同的,那么就从AL中删除AL.get(i)
*
* @param AL
* @param issuesAll
* @param a_numlimit
* @return
*/
public static List<SsqIssue> dealListWithMutiThread(ArrayList<SsqIssue> AL,ArrayList<SsqIssue> issuesAll,int a_numlimit){
int index = 0;
int dealSize = 500000;
int b=AL.size()/dealSize+1;
ExecutorService ex = Executors.newFixedThreadPool(b);
ArrayList<Future<ArrayList<SsqIssue>>> futures=new ArrayList<Future<ArrayList<SsqIssue>>>();
//分配 try {
//处理
ArrayList<SsqIssue> result = new ArrayList<SsqIssue>();
SelectResult.toFile( VeDate.getStringDate() +" begin try and get Future; result.size()="+ result.size());
for(int i=0;i<= b;i++,index+=dealSize){
int start = index;
if(start>=AL.size()) break;
int end = start + dealSize;
end = end>AL.size() ? AL.size()-1 : end;
//打日志
SelectResult.toFile( VeDate.getStringDate() +" start="+start+" end="+end);
Future<ArrayList<SsqIssue>> f = ex.submit(new Task(AL,issuesAll,start,end,a_numlimit));
futures.add(f);
}
//打日志
SelectResult.toFile(" futures.size()="+futures.size());
ArrayList<Integer> redb =new ArrayList<Integer>();
ArrayList<SsqIssue> s=new ArrayList<SsqIssue>();
for(Future<ArrayList<SsqIssue>> future : futures){
//合并操作
if(future!=null&&future.get()!=null) {s.addAll(future.get());}
result.addAll(s);
}
SelectResult.toFile( VeDate.getStringDate() +" try end; result.size()=" + result.size());
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static class Task implements Callable<ArrayList<SsqIssue>>{
private ArrayList<SsqIssue> AL;
private ArrayList<SsqIssue> issuesAll;
private int start;
private int end;
private int a_numlimit;
public Task(ArrayList<SsqIssue> ALl,ArrayList<SsqIssue> issuesAll,int start,int end,int a_numlimit){
this.AL = new ArrayList<SsqIssue>(ALl.subList(start, end));
for(int i=start;i<ALl.size()&&i<end;i++){
this.AL.add(ALl.get(i));
}
this.issuesAll=issuesAll;
this.start = start;
this.end = end;
this.a_numlimit=a_numlimit;
}
public ArrayList<SsqIssue> call() throws Exception {
SelectResult.toFile( VeDate.getStringDate() +"Task("+ this.AL.size()+" ,"+ issuesAll.size()+","+ start+","+ end+","+ a_numlimit+")" );
for(int i=0;i<this.AL.size();i++){
log("AL.size()="+this.AL.size());
for(int j=0;j<issuesAll.size();j++){
//log("Thread.currentThread().getName()="+Thread.currentThread().getName() +"|| AL.size()="+AL.size());
SsqIssue SI=new SsqIssue();
SI.setBlueBall(this.AL.get(i).getBlueBall());
SI.setRedBalls(this.AL.get(i).getRedBalls());
SI.setIssue(this.AL.get(i).getIssue());
SI.setIdx(this.AL.get(i).getIdx());
if(mycontains(SI.getRedBalls(),issuesAll.get(j).getRedBalls(),a_numlimit)){
if(this.AL.get(i)!=null){
this.AL.remove(SI);
}
}
}
}
//返回处理结果
return this.AL;
}
}调用多线程代码如下: List<SsqIssue> AL2=new ArrayList<SsqIssue>();
AL2=dealListWithMutiThread(AL,issuesAll,a_numlimit);
log("!!!AL2.size()="+AL2);
控制台报错信息如下:
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
java.util.concurrent.ExecutionException: java.lang.NullPointerException
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: !!!AL2.size()=null
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
at java.util.concurrent.FutureTask.report(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at com.fh.xt.MySsqStaticstic.dealListWithMutiThread(MySsqStaticstic.java:3376)
at com.fh.xt.MySsqStaticstic.selectSsqLL(MySsqStaticstic.java:1444)
at com.fh.xt.MySsqStaticstic.main(MySsqStaticstic.java:203)
Caused by: java.lang.NullPointerException
at com.fh.xt.MySsqStaticstic$Task.call(MySsqStaticstic.java:3414)
at com.fh.xt.MySsqStaticstic$Task.call(MySsqStaticstic.java:1)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
Exception in thread "main" java.lang.NullPointerException
at java.util.ArrayList.addAll(Unknown Source)
at com.fh.xt.MySsqStaticstic.selectSsqLL(MySsqStaticstic.java:1447)
at com.fh.xt.MySsqStaticstic.main(MySsqStaticstic.java:203)请问各位何解???
/**多线程处理ArrayList<SsqIssue> AL
* AL.get(i).getRedBalls()与 issuesAll.get(j).getRedBalls()比较,有a_numlimit个数据相同的,那么就从AL中删除AL.get(i)
*
* @param AL
* @param issuesAll
* @param a_numlimit
* @return
*/
public static List<SsqIssue> dealListWithMutiThread(ArrayList<SsqIssue> AL,ArrayList<SsqIssue> issuesAll,int a_numlimit){
int index = 0;
int dealSize = 500000;
int b=AL.size()/dealSize+1;
ExecutorService ex = Executors.newFixedThreadPool(b);
ArrayList<Future<ArrayList<SsqIssue>>> futures=new ArrayList<Future<ArrayList<SsqIssue>>>();
//分配 try {
//处理
ArrayList<SsqIssue> result = new ArrayList<SsqIssue>();
SelectResult.toFile( VeDate.getStringDate() +" begin try and get Future; result.size()="+ result.size());
for(int i=0;i<= b;i++,index+=dealSize){
int start = index;
if(start>=AL.size()) break;
int end = start + dealSize;
end = end>AL.size() ? AL.size()-1 : end;
//打日志
SelectResult.toFile( VeDate.getStringDate() +" start="+start+" end="+end);
Future<ArrayList<SsqIssue>> f = ex.submit(new Task(AL,issuesAll,start,end,a_numlimit));
futures.add(f);
}
//打日志
SelectResult.toFile(" futures.size()="+futures.size());
ArrayList<Integer> redb =new ArrayList<Integer>();
ArrayList<SsqIssue> s=new ArrayList<SsqIssue>();
for(Future<ArrayList<SsqIssue>> future : futures){
//合并操作
if(future!=null&&future.get()!=null) {s.addAll(future.get());}
result.addAll(s);
}
SelectResult.toFile( VeDate.getStringDate() +" try end; result.size()=" + result.size());
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static class Task implements Callable<ArrayList<SsqIssue>>{
private ArrayList<SsqIssue> AL;
private ArrayList<SsqIssue> issuesAll;
private int start;
private int end;
private int a_numlimit;
public Task(ArrayList<SsqIssue> ALl,ArrayList<SsqIssue> issuesAll,int start,int end,int a_numlimit){
this.AL = new ArrayList<SsqIssue>(ALl.subList(start, end));
for(int i=start;i<ALl.size()&&i<end;i++){
this.AL.add(ALl.get(i));
}
this.issuesAll=issuesAll;
this.start = start;
this.end = end;
this.a_numlimit=a_numlimit;
}
public ArrayList<SsqIssue> call() throws Exception {
SelectResult.toFile( VeDate.getStringDate() +"Task("+ this.AL.size()+" ,"+ issuesAll.size()+","+ start+","+ end+","+ a_numlimit+")" );
for(int i=0;i<this.AL.size();i++){
log("AL.size()="+this.AL.size());
for(int j=0;j<issuesAll.size();j++){
//log("Thread.currentThread().getName()="+Thread.currentThread().getName() +"|| AL.size()="+AL.size());
SsqIssue SI=new SsqIssue();
SI.setBlueBall(this.AL.get(i).getBlueBall());
SI.setRedBalls(this.AL.get(i).getRedBalls());
SI.setIssue(this.AL.get(i).getIssue());
SI.setIdx(this.AL.get(i).getIdx());
if(mycontains(SI.getRedBalls(),issuesAll.get(j).getRedBalls(),a_numlimit)){
if(this.AL.get(i)!=null){
this.AL.remove(SI);
}
}
}
}
//返回处理结果
return this.AL;
}
}调用多线程代码如下: List<SsqIssue> AL2=new ArrayList<SsqIssue>();
AL2=dealListWithMutiThread(AL,issuesAll,a_numlimit);
log("!!!AL2.size()="+AL2);
控制台报错信息如下:
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
java.util.concurrent.ExecutionException: java.lang.NullPointerException
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: !!!AL2.size()=null
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
at java.util.concurrent.FutureTask.report(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at com.fh.xt.MySsqStaticstic.dealListWithMutiThread(MySsqStaticstic.java:3376)
at com.fh.xt.MySsqStaticstic.selectSsqLL(MySsqStaticstic.java:1444)
at com.fh.xt.MySsqStaticstic.main(MySsqStaticstic.java:203)
Caused by: java.lang.NullPointerException
at com.fh.xt.MySsqStaticstic$Task.call(MySsqStaticstic.java:3414)
at com.fh.xt.MySsqStaticstic$Task.call(MySsqStaticstic.java:1)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
MySsqStaticstic: AL.size()=1429478
Exception in thread "main" java.lang.NullPointerException
at java.util.ArrayList.addAll(Unknown Source)
at com.fh.xt.MySsqStaticstic.selectSsqLL(MySsqStaticstic.java:1447)
at com.fh.xt.MySsqStaticstic.main(MySsqStaticstic.java:203)请问各位何解???
dealListWithMutiThread的作用就是 将AL分做500000条的多个TASK来多线程并行处理,然后将处理结果合并并返回。