ThreadPoolExecutor and using priority to determine what threads will run
Hello,
I am fairly new to Java, so I apologize if there is an obvious answer.
The ThreadPoolExecutor is a great class for thread management. What I would like to do is have a priority scheme that would determine what thread should be run next. For example, let's say I have 100 threads and a limit of 20 threads. Each thread that is executed should have a mechanism to indicate a priority. When a thread becomes available in the pool, the priority value should be checked to determine which thread to run. This would allow me to get important tasks completed quickly. Right now it appears that there is a FIFO order to threads. I tried wrapping the runnable object in a Thread, but that didn't seem to have an effect.
I am using a Fixed thread Pool:
loThreadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(MAXTHREADS); (MAXTHREADS=20)
I have tried creating a PriorityBlockingQueue with a comparator that compares the thread priority, but nothing seems to work.
public Constructor(){
loThreadPoolExecutor =new ThreadPoolExecutor(MAXTHREADS,MAXTHREADS*2,0,TimeUnit.SECONDS,new PriorityBlockingQueue(11,new PrvComparator()));
}
privateclass PrvComparatorimplements Comparator{
publicint compare(Object o1, Object o2){
// TODO Auto-generated method stub
Thread t1 = (Thread) o1;
Thread t2 = (Thread) o2;
System.out.println("P1="+t1.getPriority()+" P2="+t2.getPriority());
if (t1.getPriority()<t2.getPriority())return -1;
if (t1.getPriority()>t2.getPriority())return 1;
elsereturn 0;
//PriorityBlockingQueue
}
I would appreciate any help.

