Skip to content

Commit 84d1278

Browse files
stepanchegslandelle
authored andcommitted
Allow null executor in ListenableFuture
It allows execution of future callback in completion thread, and makes `ListenableFuture` closer to `CompletableFuture` (which allows `null` executor). It also saves a little memory in `toCompletableFuture` implementation.
1 parent 31864fa commit 84d1278

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

client/src/main/java/org/asynchttpclient/ListenableFuture.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public interface ListenableFuture<V> extends Future<V> {
6767
* to the executor} for execution when the {@code Future}'s computation is
6868
* {@linkplain Future#isDone() complete}.
6969
* <br>
70+
* Executor can be <code>null</code>, in that case executor will be executed
71+
* in the thread where completion happens.
72+
* <br>
7073
* There is no guaranteed ordering of execution of listeners, they may get
7174
* called in the order they were added and they may get called out of order,
7275
* but any listener added through this method is guaranteed to be called once
@@ -131,7 +134,11 @@ public void touch() {
131134

132135
@Override
133136
public ListenableFuture<T> addListener(Runnable listener, Executor exec) {
134-
exec.execute(listener);
137+
if (exec != null) {
138+
exec.execute(listener);
139+
} else {
140+
listener.run();
141+
}
135142
return this;
136143
}
137144

client/src/main/java/org/asynchttpclient/future/ExecutionList.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public void add(Runnable runnable, Executor executor) {
5656
// Fail fast on a null. We throw NPE here because the contract of Executor states that it
5757
// throws NPE on null listener, so we propagate that contract up into the add method as well.
5858
assertNotNull(runnable, "runnable");
59-
assertNotNull(executor, "executor");
6059

6160
// Lock while we check state. We must maintain the lock while adding the new pair so that
6261
// another thread can't run the list out from under us. We only add to the list if we have not
@@ -122,7 +121,11 @@ public void execute() {
122121
*/
123122
private static void executeListener(Runnable runnable, Executor executor) {
124123
try {
125-
executor.execute(runnable);
124+
if (executor != null) {
125+
executor.execute(runnable);
126+
} else {
127+
runnable.run();
128+
}
126129
} catch (RuntimeException e) {
127130
// Log it and keep going, bad runnable and/or executor. Don't punish the other runnables if
128131
// we're given a bad one. We only catch RuntimeException because we want Errors to propagate

client/src/main/java/org/asynchttpclient/netty/NettyResponseFuture.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,7 @@ public void run() {
271271
completable.complete((V) CONTENT_UPDATER.get(NettyResponseFuture.this));
272272
}
273273

274-
}, new Executor() {
275-
@Override
276-
public void execute(Runnable command) {
277-
command.run();
278-
}
279-
});
274+
}, null);
280275

281276
return completable;
282277
}

0 commit comments

Comments
 (0)