在Java中指定任务顺序执行

时间:2012-10-04 07:37:31

标签: java multithreading threadpool threadpoolexecutor

我搜索了很多但找不到任何解决方案。 我以这种方式使用java线程池:

ExecutorService c = Executors.newFixedThreadPool(3);
for (int i = 0; i < 10; ++i) {
  c.execute(new MyTask(i));
}

以这种方式,任务以后续顺序执行(如在队列中)。但我需要改变“选择下一个任务”策略。所以我希望为每个任务分配指定优先级(它不是线程优先级),并且执行任务对应于这些优先级。因此,当执行程序完成另一个任务时,它会将下一个任务选为具有最高优先级的任务。 它描述了常见问题。也许有更简单的方法不考虑优先级。它选择最后添加的任务作为执行而不是第一次添加。粗略地讲,FixedThreadPool使用FIFO策略。我可以使用例如LIFO策略吗?

2 个答案:

答案 0 :(得分:12)

您可以使用PriorityBlockingQueue指定Queue to ThreadPoolExecutor。

public class PriorityExecutor extends ThreadPoolExecutor {

    public PriorityExecutor(int corePoolSize, int maximumPoolSize,
            long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }
    //Utitlity method to create thread pool easily
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new PriorityExecutor(nThreads, nThreads, 0L,
                TimeUnit.MILLISECONDS, new PriorityBlockingQueue<Runnable>());
    }
    //Submit with New comparable task 
    public Future<?> submit(Runnable task, int priority) {
        return super.submit(new ComparableFutureTask(task, null, priority));
    }
    //execute with New comparable task 
    public void execute(Runnable command, int priority) {
        super.execute(new ComparableFutureTask(command, null, priority));
    }
}

定义ComparableFutureTask以比较优先级。

class ComparableFutureTask<T> extends FutureTask<T>
        implements
            Comparable<ComparableFutureTask<T>> {

    volatile int priority = 0;

    public ComparableFutureTask(Runnable runnable, T result, int priority) {
        super(runnable, result);
        this.priority = priority;
    }
    public ComparableFutureTask(Callable<T> callable, int priority) {
        super(callable);
        this.priority = priority;
    }
    @Override
    public int compareTo(ComparableFutureTask<T> o) {
        return Integer.valueOf(priority).compareTo(o.priority);
    }
  }

答案 1 :(得分:7)

ThreadPoolExecutor构造函数接受BlockingQueue。 您可以将队列作为PriorityBlockingQueue传递。在订购时,您不需要通过自定义比较器维护订单。

static BlockingQueue<Task> queue=new PriorityBlockingQueue<Task>(MAXPOOL,new TaskComparator());

static ThreadPoolExecutor threadpool = new ThreadPoolExecutor(30, MAXPOOL, 
        MAXPOOL, TimeUnit.SECONDS, (PriorityBlockingQueue) queue, new mThreadFactory());



class TaskComparator implements Comparator<Task>{
  public int compare(Task t1, Task t2){
    //write you own logic to compare two task.
  }
}