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.)

  1. abstractlisteningexecutorservice doesn't allow override newtaskfor. (why? can explain if you'd file feature request.) result, you'll have extend abstractexecutorservice directly, largely duplicating (short) abstractlisteningexecutorservice implementation.
  2. newtaskfor has return listenablefuture that's comparable. obvious choice listenablefuture listenablefuturetask, class final, can't make instances comparable. solution create listenablefuturetask , wrap in simpleforwardinglistenablefuture implements comparable.

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

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -