1

我正在尝试安排一堆任务定期执行。在某些情况下,某些任务需要停止调度,所以我将它们从 threadPoolExecutor 的内部队列中删除。我从任务本身中做到这一点

下面是我的方法。我不确定从 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;    
    }
    }
4

2 回答 2

2

每当您安排任务时

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
     */ 
于 2013-07-17T04:56:06.873 回答
0

强行取消任务是危险的,这就是为什么 stop 是从 java 中删除的标记,所以,或者你应该在你的线程中有一个共享标志......

比如:我可以活吗?我可以活吗?不?好的返回!这个接缝很大但是是安全的方式!

于 2013-07-16T23:53:12.503 回答