Skip to content

Commit ae0d945

Browse files
committed
DeferredResult accessors based on volatile fields for proper visibility
Issue: SPR-13451
1 parent 4dee9cb commit ae0d945

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

spring-web/src/main/java/org/springframework/web/context/request/async/DeferredResult.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.web.context.request.async;
1718

1819
import java.util.PriorityQueue;
@@ -64,9 +65,9 @@ public class DeferredResult<T> {
6465

6566
private DeferredResultHandler resultHandler;
6667

67-
private Object result = RESULT_NONE;
68+
private volatile Object result = RESULT_NONE;
6869

69-
private boolean expired;
70+
private volatile boolean expired;
7071

7172

7273
/**
@@ -98,33 +99,34 @@ public DeferredResult(Long timeout, Object timeoutResult) {
9899
this.timeout = timeout;
99100
}
100101

102+
101103
/**
102104
* Return {@code true} if this DeferredResult is no longer usable either
103105
* because it was previously set or because the underlying request expired.
104-
* <p>
105-
* The result may have been set with a call to {@link #setResult(Object)},
106+
* <p>The result may have been set with a call to {@link #setResult(Object)},
106107
* or {@link #setErrorResult(Object)}, or as a result of a timeout, if a
107108
* timeout result was provided to the constructor. The request may also
108109
* expire due to a timeout or network error.
109110
*/
110111
public final boolean isSetOrExpired() {
111-
return ((this.result != RESULT_NONE) || this.expired);
112+
return (this.result != RESULT_NONE || this.expired);
112113
}
113114

114115
/**
115-
* @return {@code true} if the DeferredResult has been set.
116+
* Return {@code true} if the DeferredResult has been set.
116117
*/
117118
public boolean hasResult() {
118-
return this.result != RESULT_NONE;
119+
return (this.result != RESULT_NONE);
119120
}
120121

121122
/**
122-
* @return the result or {@code null} if the result wasn't set; since the result can
123-
* also be {@code null}, it is recommended to use {@link #hasResult()} first
124-
* to check if there is a result prior to calling this method.
123+
* Return the result, or {@code null} if the result wasn't set. Since the result
124+
* can also be {@code null}, it is recommended to use {@link #hasResult()} first
125+
* to check if there is a result prior to calling this method.
125126
*/
126127
public Object getResult() {
127-
return hasResult() ? this.result : null;
128+
Object resultToCheck = this.result;
129+
return (resultToCheck != RESULT_NONE ? resultToCheck : null);
128130
}
129131

130132
/**
@@ -165,12 +167,12 @@ public final void setResultHandler(DeferredResultHandler resultHandler) {
165167
Assert.notNull(resultHandler, "DeferredResultHandler is required");
166168
synchronized (this) {
167169
this.resultHandler = resultHandler;
168-
if ((this.result != RESULT_NONE) && (!this.expired)) {
170+
if (this.result != RESULT_NONE && !this.expired) {
169171
try {
170172
this.resultHandler.handleResult(this.result);
171173
}
172-
catch (Throwable t) {
173-
logger.trace("DeferredResult not handled", t);
174+
catch (Throwable ex) {
175+
logger.trace("DeferredResult not handled", ex);
174176
}
175177
}
176178
}
@@ -214,9 +216,9 @@ public boolean setErrorResult(Object result) {
214216
return setResultInternal(result);
215217
}
216218

219+
217220
final DeferredResultProcessingInterceptor getInterceptor() {
218221
return new DeferredResultProcessingInterceptorAdapter() {
219-
220222
@Override
221223
public <S> boolean handleTimeout(NativeWebRequest request, DeferredResult<S> deferredResult) {
222224
if (timeoutCallback != null) {
@@ -227,7 +229,6 @@ public <S> boolean handleTimeout(NativeWebRequest request, DeferredResult<S> def
227229
}
228230
return true;
229231
}
230-
231232
@Override
232233
public <S> void afterCompletion(NativeWebRequest request, DeferredResult<S> deferredResult) {
233234
synchronized (DeferredResult.this) {

0 commit comments

Comments
 (0)