java ScheduleExecutor服务超时任务

java ScheduleExecutor服务超时任务,java,scheduledexecutorservice,Java,Scheduledexecutorservice,我必须在某个时间间隔安排一些任务,并且必须终止超过指定时间的任务 代码: public class ExecutorMain { public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException { ScheduledThreadPoolExecutor scheduledExecutorService = new Sc

我必须在某个时间间隔安排一些任务,并且必须终止超过指定时间的任务

代码:

public class ExecutorMain {
  public static void main(String[] args) throws InterruptedException, ExecutionException,        TimeoutException {
    ScheduledThreadPoolExecutor scheduledExecutorService = new     ScheduledThreadPoolExecutor(2);
    scheduledExecutorService.scheduleAtFixedRate(new ShortTask(2), 0, 3, TimeUnit.SECONDS);
    scheduledExecutorService.scheduleAtFixedRate(new LongTask(1), 0, 10, TimeUnit.SECONDS);
  }
}

class ShortTask implements Runnable {
  private int id;
  ShortTask(int id) {
    this.id = id;
  }
  public void run() {
    try {
        Thread.sleep(1000);
        System.out.println("Short Task with id "+id+" executed by "+Thread.currentThread().getId());
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
  }
}

class LongTask implements Runnable {
  private int id;
  LongTask(int id) {
    this.id = id;
  }
  public void run() {
    try {
        Thread.sleep(6000);
        System.out.println("Long Task with id "+id+" executed by "+Thread.currentThread().getId());
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
  }
}

在上面的示例中,我希望在不干扰其他任务执行的情况下终止耗时超过5秒(长任务)的任务。实现这一点的最佳方法是什么?

使用
ScheduledFuture
来处理它。试试看

final ScheduledFuture<?> longTaskHandler=
          scheduledExecutorService.scheduleAtFixedRate(new LongTask(1), 
           0, 10, TimeUnit.SECONDS);

scheduledExecutorService.schedule(new Runnable() {
        public void run() {
            longTaskHandler.cancel(true);
        // This will cancel your LongTask after 5 sec without effecting ShortTask
        }
    }, 5, TimeUnit.SECONDS);
longTaskHandler最终计划未来=
scheduledExecutorService.scheduleAtFixedRate(新长任务(1),
0,10,时间单位(秒);
scheduledExecutorService.schedule(新的Runnable(){
公开募捐{
longTaskHandler.cancel(真);
//这将在5秒后取消你的长任务而不影响短任务
}
},5,时间单位(秒);

有关详细信息,请查看此项。

使用
ScheduledFuture
来处理它。试试看

final ScheduledFuture<?> longTaskHandler=
          scheduledExecutorService.scheduleAtFixedRate(new LongTask(1), 
           0, 10, TimeUnit.SECONDS);

scheduledExecutorService.schedule(new Runnable() {
        public void run() {
            longTaskHandler.cancel(true);
        // This will cancel your LongTask after 5 sec without effecting ShortTask
        }
    }, 5, TimeUnit.SECONDS);
longTaskHandler最终计划未来=
scheduledExecutorService.scheduleAtFixedRate(新长任务(1),
0,10,时间单位(秒);
scheduledExecutorService.schedule(新的Runnable(){
公开募捐{
longTaskHandler.cancel(真);
//这将在5秒后取消你的长任务而不影响短任务
}
},5,时间单位(秒);

有关详细信息,请查看此项。

经过深思熟虑和大量搜索,使用以下方法

解决方案来自:


经过深思熟虑和大量搜索,采用以下方法

解决方案来自:


我想到了这一点,但是如果我有多个任务需要超过超时时间才能执行,那么我必须将我的线程共享给这个取消任务。如果任务知道超时,那么我们就不必安排线程来检查超时情况。@SatishM,这只会影响你的长任务,短任务将继续工作。上面的示例是2个任务,但实际上我有多个任务,因此我必须为所有主任务安排此取消任务(我不知道哪项任务实际需要比超时更多的时间)。我想到了这一点,但如果我有多个任务需要超过超时时间才能执行,那么我必须将我的线程共享到此取消任务。如果任务知道其超时,则我们不必安排线程检查超时。@SatishM,这只会影响长任务,短任务将继续工作。上面的示例是taking 2个任务,但实际上我有多个任务,因此我必须为所有主要任务安排此取消任务(我不知道哪个任务实际需要的时间比超时时间更长)。