First of all: I already read the following two questions and their possible solutions:
The dilemma I'm having is that I want to use a custom BlockingQueue or rather a different but specific queue, namely PriorityBlockingQueue with a custom Comparator which sorts the queue by priority.
The ThreadPoolExecutor does support custom queues in its constructors, but it does not implement the methods from the ScheduledExecutorService interface. So I went and found the subclass ScheduledThreadPoolExecutor, but it does not support custom queues and uses a DelayedWorkQueue instead.
Problems:
- I cannot extend from
ScheduledThreadPoolExecutorbecause creating constructors for my own class won't do anything since the constructors of theScheduledThreadPoolExecutordon't accept custom queues as a parameter. - I cannot copy the contents of the
ThreadPoolExecutorclass and the implementations of theScheduledThreadPoolExecutorbecause it uses a lot of methods which are declared with no modifiers (e. g.canRunInCurrentState(boolean periodic)and all method being invoked by this call) which does not allow me to access the method since even though its a subclass ofThreadPoolExecutor, it is not in the same package.
My current implementation looks like this:
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.croemheld.tasks.PriorityTaskComparator;
public class ScheduledPriorityThreadPoolExecutor extends ThreadPoolExecutor implements ScheduledExecutorService {
private static final int INITIAL_QUEUE_SIZE = 10;
public ScheduledPriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
TimeUnit unit, BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
new PriorityBlockingQueue<Runnable>(INITIAL_QUEUE_SIZE, new PriorityTaskComparator()));
}
public ScheduledPriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
new PriorityBlockingQueue<Runnable>(INITIAL_QUEUE_SIZE, new PriorityTaskComparator()), handler);
}
public ScheduledPriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
new PriorityBlockingQueue<Runnable>(INITIAL_QUEUE_SIZE, new PriorityTaskComparator()), threadFactory);
}
public ScheduledPriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
new PriorityBlockingQueue<Runnable>(INITIAL_QUEUE_SIZE, new PriorityTaskComparator()), threadFactory, handler);
}
@Override
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
// TODO Auto-generated method stub
return null;
}
@Override
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
// TODO Auto-generated method stub
return null;
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
// TODO Auto-generated method stub
return null;
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
// TODO Auto-generated method stub
return null;
}
}
As you can see, the constructors problem is solved, but it still leaves the implementations of the scheduling methods from ScheduledExecutorService.
So I'm asking you, is there any way to maybe pass a Comparator to the queue or a simple and not too exhaustive way to create an own executor class which implements the methods from ScheduledExecutorService and offers the methods from the ThreadPoolExecutor class as well as uses a PriorityBlockingQueue?