public static voidmain(String[] args) {int corePoolSize = 3;int maximumPoolSize = Runtime.getRuntime().availableProcessors() * 2;
BlockingQueue queue = new ArrayBlockingQueue<>(512);
RejectedExecutionHandler policy= new ThreadPoolExecutor.DiscardPolicy(); //什么也不做,直接忽略
ExecutorService threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 0, TimeUnit.SECONDS, queue, policy);
submitTisks(gatherDate, hbaseConfig, corePoolSize, threadPoolExecutor);
}/*** 提交任务
*@paramgatherDate
*@paramhbaseConfig
*@paramcorePoolSize
*@paramthreadPoolExecutor*/
private static void submitTisks(String gatherDate, Configuration hbaseConfig, intcorePoolSize, ExecutorService threadPoolExecutor) {
List> taskFutureList = new ArrayList<>();//提交任务,任务的执行由线程池去调用执行并管理。
Future gpsfuture = threadPoolExecutor.submit(newGPSTask(hbaseConfig, gatherDate, xikangTidList, rootPath));//这里获取结果任务的Future,并放到list中,供所有任务提交完后,通过每个任务的Future判断执行状态和结果。
taskFutureList.add(gpsfuture);int done = 0; //完成任务的数量
while (!taskFutureList.isEmpty()) {
Iterator> iter =taskFutureList.iterator();while(iter.hasNext()) {
Future fut =iter.next();if(fut.isDone()) {try{
Boolean flag=fut.get();if (flag){ done++;}
}catch(Exception e){
e.printStackTrace();
}
iter.remove();
}
}
}
}/************************************使用Callable接口**************************************/
static class GPSTask implements Callable{
Configuration config;
String recordDate;
ListxkDatanoList;
String rootPath;
Thread currentThread;public GPSTask(Configuration config, String recordDate, ListxkDatanoList,String rootPath) {this.config =config;this.recordDate =recordDate;this.xkDatanoList =xkDatanoList;this.rootPath =rootPath;
}
@Overridepublic Boolean call() throwsException {this.currentThread =Thread.currentThread();returnFindDataUtil.getData4HbaseGPSSQ(config, recordDate, xkDatanoList,rootPath,currentThread);
}
}