多线程ExecutorService使用实例

 

 

public interface ListCollectionCallback {
	public void call(List collection);
}

 

 

实现Runnable,处理业务逻辑

 

public class ProcessDataFutureCallable implements Runnable {

	@Override
	public void run() {
		//处理业务逻辑
	}

}

  多线程异步处理业务,通过future同步返回

 

 

	final List> dataList = new ArrayList>();	
        final ExecutorService exePool = Executors.newFixedThreadPool(threadNum);
		ListCollectionCallback callable = new ListCollectionCallback() {
			@Override
			public void call(List collection) {
				ProcessDataFutureCallable future=new ProcessDataFutureCallable();
				dataList.add(exePool.submit(future));
			}
		};
		//剥离业务调用callable
		CollectionUtils.splitCollectionHandle(callable);
		for (Future future : dataList) {
			try {
				future.get();
			} catch (InterruptedException e) {
				logger.warn("线程异常中断", e);
			} catch (ExecutionException e) {
				logger.warn("线程异常中断", e);
			} catch (Exception e) {
				logger.warn("发生异常", e);
			}
		}
		exePool.shutdown();

 

 

 

你可能感兴趣的:(JAVA)