ExecutorService that interrupts tasks after a timeout

后端 未结 9 2318
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 16:19

I\'m looking for an ExecutorService implementation that can be provided with a timeout. Tasks that are submitted to the ExecutorService are interrupted if they take longer t

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 16:52

    check if this works for you,

        public  ResponseObject>> runOnScheduler(ThreadPoolExecutor threadPoolExecutor,
          int parallelismLevel, TimeUnit timeUnit, int timeToCompleteEachTask, Collection collection,
          Map context, Task someTask){
        if(threadPoolExecutor==null){
          return ResponseObject.>>builder().errorCode("500").errorMessage("threadPoolExecutor can not be null").build();
        }
        if(someTask==null){
          return ResponseObject.>>builder().errorCode("500").errorMessage("Task can not be null").build();
        }
        if(CollectionUtils.isEmpty(collection)){
          return ResponseObject.>>builder().errorCode("500").errorMessage("input collection can not be empty").build();
        }
    
        LinkedBlockingQueue> callableLinkedBlockingQueue = new LinkedBlockingQueue<>(collection.size());
        collection.forEach(value -> {
          callableLinkedBlockingQueue.offer(()->someTask.perform(value,context)); //pass some values in callable. which can be anything.
        });
        LinkedBlockingQueue> futures = new LinkedBlockingQueue<>();
    
        int count = 0;
    
        while(count f = threadPoolExecutor.submit(callableLinkedBlockingQueue.poll());
          futures.offer(f);
          count++;
        }
    
        Collection> responseCollection = new ArrayList<>();
    
        while(futures.size()>0){
          Future future = futures.poll();
          ResponseObject responseObject = null;
            try {
              T response = future.get(timeToCompleteEachTask, timeUnit);
              responseObject = ResponseObject.builder().data(response).build();
            } catch (InterruptedException e) {
              future.cancel(true);
            } catch (ExecutionException e) {
              future.cancel(true);
            } catch (TimeoutException e) {
              future.cancel(true);
            } finally {
              if (Objects.nonNull(responseObject)) {
                responseCollection.add(responseObject);
              }
              futures.remove(future);//remove this
              Callable callable = getRemainingCallables(callableLinkedBlockingQueue);
              if(null!=callable){
                Future f = threadPoolExecutor.submit(callable);
                futures.add(f);
              }
            }
    
        }
        return ResponseObject.>>builder().data(responseCollection).build();
      }
    
      private  Callable getRemainingCallables(LinkedBlockingQueue> callableLinkedBlockingQueue){
        if(callableLinkedBlockingQueue.size()>0){
          return callableLinkedBlockingQueue.poll();
        }
        return null;
      }
    

    you can restrict the no of thread uses from scheduler as well as put timeout on the task.

提交回复
热议问题