相关疑难解决方法(0)

处理ThreadPoolExecutor的异常

我有以下代码片段,它基本上扫描需要执行的任务列表,然后将每个任务提供给执行程序执行.

JobExecutor反过来又创造了另一个执行人(做数据库的东西...读取和写入数据队列),并完成任务.

JobExecutor返回Future<Boolean>提交的任务.当其中一个任务失败时,我想优雅地中断所有线程并通过捕获所有异常来关闭执行程序.我需要做哪些改变?

public class DataMovingClass {
    private static final AtomicInteger uniqueId = new AtomicInteger(0);

  private static final ThreadLocal<Integer> uniqueNumber = new IDGenerator();   

  ThreadPoolExecutor threadPoolExecutor  = null ;

   private List<Source> sources = new ArrayList<Source>();

    private static class IDGenerator extends ThreadLocal<Integer> {
        @Override
        public Integer get() {
            return uniqueId.incrementAndGet();
        }
  }

  public void init(){

    // load sources list

  }

  public boolean execute() {

    boolean succcess = true ; 
    threadPoolExecutor = new ThreadPoolExecutor(10,10,
                10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1024), …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading exception-handling threadpoolexecutor

17
推荐指数
2
解决办法
2万
查看次数