2013-07-16 2 views
1

Я пытаюсь запланировать набор задач для выполнения периодически. в определенных ситуациях некоторые задачи необходимо остановить из планирования, поэтому я удаляю их из промежуточной очереди threadPoolExecutor. Я делаю это из самой задачиScheduledThreadPoolExecutor.remove: Безопасно ли использовать

Ниже мой подход. Я не уверен, что идея удалить задачу из службы threadPoolExecutor изнутри задачи может вызвать любую проблему. (Посмотрите на имя синхронизированного метода «removeTask». Есть ли лучший способ выполнить то, что я пытаюсь сделать здесь .

public class SchedulerDaemon { 


    private ScheduledExecutorService taskScheduler; 
    private ScheduledFuture taskResult1, taskResult2; 
    private Task1 task1; 
    private Task2 task2; 


    public SchedulerDaemon(Task1 task, Task2 task2) 
    { 
     this.task1 = task1; 
     this.task2 = task2;1 
     taskScheduler = new ScheduledThreadPoolExecutor(1); 
    } 


    public void start() { 
     if(taskScheduler == null) { 
      taskScheduler = new ScheduledThreadPoolExecutor(1); 
      taskResult = taskScheduler.scheduleAtFixedRate(new TaskWrapper(task1) , 60000,60000, TimeUnit.MILLISECONDS); 
      taskResult2 = taskScheduler.scheduleAtFixedRate(new TaskWrapper(task2) , 60000,60000, TimeUnit.MILLISECONDS); 

     } 
    } 


    public void stop() { 
     if(taskScheduler != null) { 
      taskScheduler.shutdown(); 
      taskResult1.cancel(false); 
      taskResult2.cancel(false);   
      taskScheduler = null; 
      taskResult = null; 
     } 

    } 

     public synchronized void removeTask(TaskWrapper task){ 
      ((ScheduledThreadPoolExecutor) taskScheduler).remove(task); 
     } 

    class TaskWrapper implements Runnable { 
     private Task myTask; 

     public TaskWrapper(Task task) { 
      myTask = task; 
     } 

     @Override 
     public void run() { 
      try { 
       boolean keepRunningTask = myTask.call(); 
       if(!keepRunningTask) { 

        ***//Should this cause any problem??*** 
        removeTask(this); 
       } 
      } catch (Exception e) { 
       //the task threw an exception remove it from execution queue 
       ***//Should this cause any problem??*** 
       removeTask(this); 
      } 
     } 



    } 
} 


public Task1 implements Callable<Boolean> { 

public Boolean call() { 
if(<something>) 
return true; 
else 
return false;  
} 
} 



public Task2 implements Callable<Boolean> { 

    public Boolean call() { 
    if(<something>) 
    return true; 
    else 
    return false;  
    } 
    } 

ответ

0

Отмена задания силой опасно, поэтому остановка знак, чтобы удалить из Явы, так, в альтернативе, вы должны иметь общий флаг в вашей теме ...

что-то вроде : могу ли я жить? Могу ли я жить? Нет, хорошо, верните этот шов, но это безопасный путь!

+0

я не отменяющий задачи, я удалить его из внутренней очереди ScheduledThreadPoolExecutor, я не уверен, что вы имеете в виду, имея общий флаг и как будет ли это касаться того, что я пытаюсь сделать, не могли бы вы привести некоторые примеры кода? – user2562732

1

Всякий раз, когда вы планируете a Задача

ScheduledFuture<?> future = schedulerService.scheduleAtFixedRate(new AnyTask()); 

Будущий объект возвращается. Используйте этот объект будущего для отмены этой задачи. попробовать это

future.cancel(true); 

из JavaDocs

/** 
    * Attempts to cancel execution of this task. This attempt will 
    * fail if the task has already completed, has already been cancelled, 
    * or could not be cancelled for some other reason. If successful, 
    * and this task has not started when <tt>cancel</tt> is called, 
    * this task should never run. If the task has already started, 
    * then the <tt>mayInterruptIfRunning</tt> parameter determines 
    * whether the thread executing this task should be interrupted in 
    * an attempt to stop the task. 
    * 
    * <p>After this method returns, subsequent calls to {@link #isDone} will 
    * always return <tt>true</tt>. Subsequent calls to {@link #isCancelled} 
    * will always return <tt>true</tt> if this method returned <tt>true</tt>. 
    * 
    * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this 
    * task should be interrupted; otherwise, in-progress tasks are allowed 
    * to complete 
    * @return <tt>false</tt> if the task could not be cancelled, 
    * typically because it has already completed normally; 
    * <tt>true</tt> otherwise 
    */ 
Смежные вопросы