guava - How can I use PriorityBlockingQueue with ListeningExecutorService? -
since guava's listeningexecutorservice implemented wrapping existing executorservice, 'decorates' task intercepting execute() method. means if want use custom priorityqueue on underlying executorservice, comparator "sees" decorated task listenablefuturetask object instead of original.
is there way hold of task wraps? queue's comparator can use tasks weight determine ordering?
i assume you're concerned submit()
rather execute()
? (see bottom of response.)
with listeningexecutorservice
moreexecutors.listeningdecorator
(the kind of wrapper refer to), you're out of luck. listeningdecorator
, executorservice
implementations, wraps input submit
in futuretask
. normal solution problem implement abstractexecutorservice
, override newtaskfor
return custom object. should work here, too. you'll reimplementing listeningdecorator
, trivial wrapper around abstractlisteningexecutorservice
, trivial wrapper around abstractexecutorservice
.
there 2 couple complications. (ok, there might more. admit haven't tested approach i'm suggesting.)
abstractlisteningexecutorservice
doesn't allow overridenewtaskfor
. (why? can explain if you'd file feature request.) result, you'll have extendabstractexecutorservice
directly, largely duplicating (short)abstractlisteningexecutorservice
implementation.newtaskfor
has returnlistenablefuture
that'scomparable
. obvious choicelistenablefuture
listenablefuturetask
, classfinal
, can't make instancescomparable
. solution createlistenablefuturetask
, wrap insimpleforwardinglistenablefuture
implementscomparable
.
why assume you're dealing submit()
rather execute()
?
listeningdecorator(...).execute()
doesn't wrap input task, shown test wrote:
public void testlisteningdecorator_nowrapexecutetask() { executorservice delegate = mock(executorservice.class); listeningexecutorservice service = listeningdecorator(delegate); runnable task = new runnable() { @override public void run() {} }; service.execute(task); verify(delegate).execute(task); }
Comments
Post a Comment