这似乎是 API 中的一个限制,CompletableFuture
它没有提供直接的使用PriorityBlockingQueue
. 幸运的是,我们可以毫不费力地破解它。在 Oracle 的 1.8 JVM 中,它们恰好命名了所有内部类的 fields fn
,因此提取我们的优先级感知Runnable
s 可以毫不费力地完成:
public class CFRunnableComparator implements Comparator<Runnable> {
@Override
@SuppressWarnings("unchecked")
public int compare(Runnable r1, Runnable r2) {
// T might be AsyncSupply, UniApply, etc., but we want to
// compare our original Runnables.
return ((Comparable) unwrap(r1)).compareTo(unwrap(r2));
}
private Object unwrap(Runnable r) {
try {
Field field = r.getClass().getDeclaredField("fn");
field.setAccessible(true);
// NB: For performance-intensive contexts, you may want to
// cache these in a ConcurrentHashMap<Class<?>, Field>.
return field.get(r);
} catch (IllegalAccessException | NoSuchFieldException e) {
throw new IllegalArgumentException("Couldn't unwrap " + r, e);
}
}
}
这假设您的Supplier
班级是Comparable
,例如:
public interface WithPriority extends Comparable<WithPriority> {
int priority();
@Override
default int compareTo(WithPriority o) {
// Reverse comparison so higher priority comes first.
return Integer.compare(o.priority(), priority());
}
}
public class PrioritySupplier<T> implements Supplier<T>, WithPriority {
private final int priority;
private final Supplier<T> supplier;
public PrioritySupplier(int priority, Supplier<T> supplier) {
this.priority = priority;
this.supplier = supplier;
}
@Override
public T get() {
return supplier.get();
}
@Override
public int priority() {
return priority;
}
}
使用如下:
PriorityBlockingQueue<Runnable> q = new PriorityBlockingQueue<>(11 /*default*/,
new CFRunnableComparator());
ThreadPoolExecutor pool = new ThreadPoolExecutor(..., q);
CompletableFuture.supplyAsync(new PrioritySupplier<>(n, () -> {
...
}), pool);
如果您创建类似PriorityFunction
and的类PriorityBiConsumer
,您可以使用相同的技术来调用类似thenApplyAsync
and的方法,并且whenCompleteAsync
具有适当的优先级。