0
点赞
收藏
分享

微信扫一扫

DataX用hdfsreader导入或导出hive数据

目录

一、配置线程池

二、多线程编写代码


一、配置线程池

@Configuration
@Slf4j
public class ThreadPoolConfig {
    @Bean("threadExecutor")
    public ThreadPoolTaskExecutor threadExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = null;
        try {
            threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
            threadPoolTaskExecutor.setCorePoolSize(2); //核心线程数目
            threadPoolTaskExecutor.setMaxPoolSize(10); //指定最大线程数
            threadPoolTaskExecutor.setQueueCapacity(100);//队列中最大的数目
            threadPoolTaskExecutor.setKeepAliveSeconds(30);//线程空闲后的最大存活时间
            threadPoolTaskExecutor.setThreadNamePrefix("lowerKey-thread-executor");//线程名称前缀
            threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);//当调度器shutdown被调用时等待当前被调度的任务完成
            threadPoolTaskExecutor.initialize();
            log.info("初始化线程池成功");
        } catch (Exception e) {
            log.error("初始化线程池失败: ", e);
        }
        return threadPoolTaskExecutor;
    }
}

二、多线程编写代码

ListenableFuture<?> listenableFuture = threadPoolTaskExecutor.submitListenable(() -> {
    log.info("开一个线程");
});

listenableFuture.addCallback(result -> {
    log.info("成功回调!")
}, ex -> {
    log.error("失败回调!", ex);
});
举报

相关推荐

0 条评论