Indicate task task execution in Java

I searched a lot, but did not find any solution. I use java thread pool in this way:

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

Thus, tasks are performed in sequential order (as in the queue). But I need to change the strategy of "choose the next task." Therefore, I want to assign each task a priority (this is not a priority of the thread), and the execution of tasks corresponds to these priorities. Therefore, when the executor has finished another task, he selects the next task as the task with the highest priority. He describes a general problem. Perhaps there is a simpler approach that does not take priorities into account. He selects the last task added as the next to be executed instead of the first addition. Roughly speaking, FixedThreadPool uses the FIFO strategy. Can I use, for example, the LIFO strategy?

+14
java multithreading threadpool threadpoolexecutor
Oct 04
source share
3 answers

You can use PriorityBlockingQueue to specify a Queue for 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)); } } 

Define ComparableFutureTask to compare by priority.

 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); } } 
+11
04 Oct
source share
— -

The ThreadPoolExecutor constructor accepts a BlockingQueue. You can pass the queue as a PriorityBlockingQueue. He does not make any grantees when ordering, you need to transfer custom comparators to maintain order.

 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. } } 
+7
04 Oct. '12 at 8:05
source share

Do you want to create a LIFO artist? Hope this thread helps you.

How to create a LIFO artist

0
04 Oct
source share



All Articles