java线程提交_java线程学习

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值