Last active
July 11, 2016 09:44
-
-
Save amitshekhariitbhu/518a1d484f412225104e2a76e559e465 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PriorityThreadPoolExecutor extends ThreadPoolExecutor { | |
public PriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, | |
TimeUnit unit, ThreadFactory threadFactory) { | |
super(corePoolSize, maximumPoolSize, keepAliveTime, unit,new PriorityBlockingQueue<Runnable>(), threadFactory); | |
} | |
@Override | |
public Future<?> submit(Runnable task) { | |
PriorityFutureTask futureTask = new PriorityFutureTask((PriorityRunnable) task); | |
execute(futureTask); | |
return futureTask; | |
} | |
private static final class PriorityFutureTask extends FutureTask<PriorityRunnable> | |
implements Comparable<PriorityFutureTask> { | |
private final PriorityRunnable priorityRunnable; | |
public PriorityFutureTask(PriorityRunnable priorityRunnable) { | |
super(priorityRunnable, null); | |
this.priorityRunnable = priorityRunnable; | |
} | |
/* | |
* compareTo() method is defined in interface java.lang.Comparable and it is used | |
* to implement natural sorting on java classes. natural sorting means the the sort | |
* order which naturally applies on object e.g. lexical order for String, numeric | |
* order for Integer or Sorting employee by there ID etc. most of the java core | |
* classes including String and Integer implements CompareTo() method and provide | |
* natural sorting. | |
*/ | |
@Override | |
public int compareTo(PriorityFutureTask other) { | |
Priority p1 = priorityRunnable.getPriority(); | |
Priority p2 = other.priorityRunnable.getPriority(); | |
return p2.ordinal() - p1.ordinal(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment