mkodekar / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Make AbstractFuture a bit extension friendly #1779

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I am trying to create a new Future type called ExpirableFuture which is similar 
to SettableFuture but different in what is does on cancellation or expiry. What 
I want to do is that on cancellation, the cancellation listeners should be 
called which will be added something like this

     expirableFuture.addExpiryListener(runnable,executor)

So when someone sets the expirableFuture.expire(), the future will be cancelled 
but the expiry listeners should be called and not the main listeners.

In the AbstractFuture cancel method:

 public boolean cancel(boolean mayInterruptIfRunning) {
    if (!sync.cancel(mayInterruptIfRunning)) {
      return false;
    }
    executionList.execute(); //can be moved to protected onCancel() method
    if (mayInterruptIfRunning) {
      interruptTask();
    }
    return true;
  }

the cancel method can be made more extensible by providing a protected 
onCancel() method, so that implementer class can provide custom implementation 
of onCancel() in which I would be calling the cancel listeners.

Original issue reported on code.google.com by narendra...@gmail.com on 11 Jun 2014 at 8:53

GoogleCodeExporter commented 9 years ago
if you don't call the normal listeners then you are risking deadlock when 
downstream transforms,listeners,callbacks, etc. never get informed that the 
future is completed.

How is expiry different from cancellation?  what is your usecase?

if you just want listeners that are only called on cancellation you can do this 
with a simple delegate

e.g.

void addExpiryListener(Runnable r, Executor e) {
  addListner(new Runnable() {
    @Override public void run() {
      if (isCancelled()) {
        e.execute(r);
      }
    }
  },
  sameThreadExecutor());

Original comment by lu...@google.com on 11 Jun 2014 at 2:39

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:08

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:07