Skip to content

Commit 9666fcc

Browse files
committed
SettableListenableFuture consistently tracks cancellation state
Issue: SPR-15202
1 parent 9b26fcd commit 9666fcc

File tree

2 files changed

+103
-46
lines changed

2 files changed

+103
-46
lines changed

spring-core/src/main/java/org/springframework/util/concurrent/SettableListenableFuture.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727

2828
/**
2929
* A {@link org.springframework.util.concurrent.ListenableFuture ListenableFuture}
30-
* whose value can be set via {@link #set(Object)} or
31-
* {@link #setException(Throwable)}. It may also be cancelled.
30+
* whose value can be set via {@link #set(T)} or {@link #setException(Throwable)}.
31+
* It may also be cancelled.
3232
*
3333
* <p>Inspired by {@code com.google.common.util.concurrent.SettableFuture}.
3434
*
3535
* @author Mattias Severson
3636
* @author Rossen Stoyanchev
37+
* @author Juergen Hoeller
3738
* @since 4.1
3839
*/
3940
public class SettableListenableFuture<T> implements ListenableFuture<T> {
@@ -92,8 +93,8 @@ public void addCallback(SuccessCallback<? super T> successCallback, FailureCallb
9293

9394
@Override
9495
public boolean cancel(boolean mayInterruptIfRunning) {
95-
this.settableTask.setCancelled();
96-
boolean cancelled = this.listenableFuture.cancel(mayInterruptIfRunning);
96+
boolean cancelled = this.settableTask.setCancelled();
97+
this.listenableFuture.cancel(mayInterruptIfRunning);
9798
if (cancelled && mayInterruptIfRunning) {
9899
interruptTask();
99100
}
@@ -102,12 +103,12 @@ public boolean cancel(boolean mayInterruptIfRunning) {
102103

103104
@Override
104105
public boolean isCancelled() {
105-
return this.listenableFuture.isCancelled();
106+
return this.settableTask.isCancelled();
106107
}
107108

108109
@Override
109110
public boolean isDone() {
110-
return this.listenableFuture.isDone();
111+
return this.settableTask.isDone();
111112
}
112113

113114
/**
@@ -152,26 +153,28 @@ private static class SettableTask<T> implements Callable<T> {
152153

153154
private static final Object NO_VALUE = new Object();
154155

155-
private final AtomicReference<Object> value = new AtomicReference<>(NO_VALUE);
156+
private static final Object CANCELLED = new Object();
156157

157-
private volatile boolean cancelled = false;
158+
private final AtomicReference<Object> value = new AtomicReference<>(NO_VALUE);
158159

159160
public boolean setValue(T value) {
160-
if (this.cancelled) {
161-
return false;
162-
}
163161
return this.value.compareAndSet(NO_VALUE, value);
164162
}
165163

166164
public boolean setException(Throwable exception) {
167-
if (this.cancelled) {
168-
return false;
169-
}
170165
return this.value.compareAndSet(NO_VALUE, exception);
171166
}
172167

173-
public void setCancelled() {
174-
this.cancelled = true;
168+
public boolean setCancelled() {
169+
return this.value.compareAndSet(NO_VALUE, CANCELLED);
170+
}
171+
172+
public boolean isCancelled() {
173+
return (this.value.get() == CANCELLED);
174+
}
175+
176+
public boolean isDone() {
177+
return (this.value.get() != NO_VALUE);
175178
}
176179

177180
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)