简体   繁体   English

Future.cancel()方法中的问题

[英]Issue in Future.cancel() method

I have a below runnable task which is run by using ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); 我有一个下面的可运行任务,该任务通过使用ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); This ensures that there will be only one waiting task in the queue. 这样可以确保队列中只有一个等待任务。

protected void waitAndSweep(final String symbol) {
    try { 

        runnable = new Runnable() {
          @Override
          public void run() {
            try {
             // long sweepTime = symbolInfo.getSweepTime(symbol);
              // long timeSinceLastSweep = System.currentTimeMillis() - lastSliceSentTime;
              boolean sliceDone = Quant.equals(wave.getCommitedQuantity() % getSliceQuantity(),0);
              if(sliceDone){
                long timeSinceLastSliceQtySent = lastSliceSentTime == 0 ? getInterval() : System.currentTimeMillis() - lastSliceSentTime;
                long waitTime = timeSinceLastSliceQtySent >= getInterval() ? 0 : getInterval() - timeSinceLastSliceQtySent;
                logTradeEvent("waitAndSweep", symbol, "waittime: " + waitTime);
                if (waitTime > 0){
                  Thread.sleep(waitTime);
                }
              }

              callSweep(symbol);
            } catch(InterruptedException ie){
              ie.printStackTrace();
            }
            catch (Exception e) {
              logEvent(StrategyEntry.ERROR, "waitAndSweep", symbol,
                  "Exception caught...", e);
            }            
          }
        };

      self = threadPoolExecutor.submit(runnable);   
    }catch(RejectedExecutionException re){
      /* this exception will be thrown when wait and sweep is called more than twice.
       * threadPoolExecutor can have one running task and one waiting task.
       * */
      System.out.print(re);
      }catch (Exception e) {
      logEvent(StrategyEntry.ERROR, "waitAndSweep", symbol,
          "Exception caught...", e);
    }
  }

Consider the caller A : 考虑呼叫者A:

private void callerA(){
waitandsweep();
waitandsweep();}

this craetes two task one will be running and another waiting in the queue. 这引发了两项任务,一项正在运行,另一项在队列中等待。

Consider the callerB: 考虑呼叫者B:

private void callerB(){
self.cancel(true);
waitandsweep();}

Expecting callerB to cancel all the tasks invoked by A. Actually it is not happening.. task invoked by caller B is getting rejected because already one task is waiting in the queue. 期望调用者B取消A调用的所有任务。实际上,它没有发生。调用者B调用的任务被拒绝了,因为队列中已经有一个任务在等待。 Can you please tell why this behaviour happens? 你能告诉我为什么会发生这种情况吗?

edit 1 : How to cancel running task of executor? 编辑1:如何取消执行程序的运行任务?

The problem is, that Future.cancel(boolean) doesn't remove the task from the queue. 问题在于, Future.cancel(boolean)不会从队列中删除任务。 The Task will not be executed, once it will be pulled by the Executor but until then its still in the queue 该任务将不会被执行,一旦将被拉到Executor在队列但在那之前它仍然

try to use threadPoolExecutor.purge(); 尝试使用threadPoolExecutor.purge(); right after cancel(); cancel(); it will try to remove the canceled tasks 它将尝试删除已取消的任务

cancelling a running Task is not that easy, you could try following: call cancel(true); 取消正在运行的Task并不容易,您可以尝试执行以下操作:调用cancel(true); it will set Thread.interrupted() to true . 它将Thread.interrupted()设置为true Now in your Task check on some steps that value and so you can decide to skip next steps of your task 现在,在“任务”中检查有价值的一些步骤,因此您可以决定跳过任务的后续步骤

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM