I'm trying to add a priority queue to an existing application that uses ThreadPoolExecutor with a LinkedBlockingQueue via CompletableFuture.supplyAsync. The problem is that I can't come up with a design to assign task priorities that I can then access in PriorityBlockingQueue's Comparator. That is because my task gets wrapped up by CompletableFuture into an instance of a private inner class called AsyncSupply that hides the original task in a private field. The Comparator then gets called with these AsyncSupply objects casted as Runnables, as follows:
public class PriorityComparator<T extends Runnable> implements Comparator<T> {
@Override
public int compare(T o1, T o2) {
// T is an AsyncSupply object.
// BUT I WANT SOMETHING I CAN ASSIGN PRIORITIES TOO!
return 0;
}
}
I investigated the possibility of extending CompletableFuture so I can wrap it in a different object but so much of much of CompletableFuture is encapsulated and uninheritable. So extending it doesn't seem like an option. Nor is encapsulating it withing an adapter, as it implements a very wide interface.
I'm not sure how to approach this problem aside from copying the entire CompletableFuture, and modifying it. Any ideas?