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?
asyncSupply()
and passing anExecutor
that was configured with aLinkedBlockingQueue
? And you want to replace that withPriorityBlockingQueue
? That is, it seems like all of this is external to the thesupplyAsync()
function andCompletableFuture
. Where are you having trouble? Perhaps you could show the code that you'd like to write, with the missing bit in the middle highlighted. – Tabethatabib