You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/05/09 20:02:57 UTC
[28/34] httpcomponents-core git commit: Use atomic boolean instead of
synchronized access for state management in AbstractAsyncRequestConsumer /
AbstractAsyncResponseConsumer
Use atomic boolean instead of synchronized access for state management in AbstractAsyncRequestConsumer / AbstractAsyncResponseConsumer
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.3.x@1631089 13f79535-47bb-0310-9956-ffa450edef68
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/9341fde2
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/9341fde2
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/9341fde2
Branch: refs/heads/4.3.x
Commit: 9341fde2e4539acc35a5736fbe9d9fff95594a92
Parents: c2e2d17
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Sat Oct 11 17:46:24 2014 +0000
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Sat Oct 11 17:46:24 2014 +0000
----------------------------------------------------------------------
.../protocol/AbstractAsyncRequestConsumer.java | 55 ++++++++--------
.../protocol/AbstractAsyncResponseConsumer.java | 68 +++++++++-----------
2 files changed, 57 insertions(+), 66 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/9341fde2/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java
----------------------------------------------------------------------
diff --git a/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java b/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java
index ac706aa..b579271 100644
--- a/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java
+++ b/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java
@@ -27,6 +27,7 @@
package org.apache.http.nio.protocol;
import java.io.IOException;
+import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
@@ -40,20 +41,22 @@ import org.apache.http.protocol.HttpContext;
/**
* Abstract {@link HttpAsyncRequestConsumer} implementation that relieves its
- * subclasses form having to synchronize access to internal instance variables
- * and provides a number of protected methods that they need to implement.
+ * subclasses from having to manage internal state and provides a number of protected
+ * event methods that they need to implement.
*
* @since 4.2
*/
@ThreadSafe
public abstract class AbstractAsyncRequestConsumer<T> implements HttpAsyncRequestConsumer<T> {
- private volatile boolean completed;
+ private final AtomicBoolean completed;
+
private volatile T result;
private volatile Exception ex;
public AbstractAsyncRequestConsumer() {
super();
+ this.completed = new AtomicBoolean(false);
}
/**
@@ -122,7 +125,7 @@ public abstract class AbstractAsyncRequestConsumer<T> implements HttpAsyncReques
/**
* Use {@link #onRequestReceived(HttpRequest)} instead.
*/
- public final synchronized void requestReceived(
+ public final void requestReceived(
final HttpRequest request) throws HttpException, IOException {
onRequestReceived(request);
if (request instanceof HttpEntityEnclosingRequest) {
@@ -137,7 +140,7 @@ public abstract class AbstractAsyncRequestConsumer<T> implements HttpAsyncReques
/**
* Use {@link #onContentReceived(ContentDecoder, IOControl)} instead.
*/
- public final synchronized void consumeContent(
+ public final void consumeContent(
final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
onContentReceived(decoder, ioctrl);
}
@@ -145,36 +148,30 @@ public abstract class AbstractAsyncRequestConsumer<T> implements HttpAsyncReques
/**
* Use {@link #buildResult(HttpContext)} instead.
*/
- public final synchronized void requestCompleted(final HttpContext context) {
- if (this.completed) {
- return;
- }
- this.completed = true;
- try {
- this.result = buildResult(context);
- } catch (final Exception ex) {
- this.ex = ex;
- } finally {
- releaseResources();
+ public final void requestCompleted(final HttpContext context) {
+ if (this.completed.compareAndSet(false, true)) {
+ try {
+ this.result = buildResult(context);
+ } catch (final Exception ex) {
+ this.ex = ex;
+ } finally {
+ releaseResources();
+ }
}
}
- public final synchronized void failed(final Exception ex) {
- if (this.completed) {
- return;
+ public final void failed(final Exception ex) {
+ if (this.completed.compareAndSet(false, true)) {
+ this.ex = ex;
+ releaseResources();
}
- this.completed = true;
- this.ex = ex;
- releaseResources();
}
- public final synchronized void close() throws IOException {
- if (this.completed) {
- return;
+ public final void close() throws IOException {
+ if (this.completed.compareAndSet(false, true)) {
+ releaseResources();
+ onClose();
}
- this.completed = true;
- releaseResources();
- onClose();
}
public Exception getException() {
@@ -186,7 +183,7 @@ public abstract class AbstractAsyncRequestConsumer<T> implements HttpAsyncReques
}
public boolean isDone() {
- return this.completed;
+ return this.completed.get();
}
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/9341fde2/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java
----------------------------------------------------------------------
diff --git a/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java b/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java
index 7cc2539..3f098c0 100644
--- a/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java
+++ b/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java
@@ -27,11 +27,11 @@
package org.apache.http.nio.protocol;
import java.io.IOException;
+import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
-import org.apache.http.annotation.ThreadSafe;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.IOControl;
@@ -39,20 +39,21 @@ import org.apache.http.protocol.HttpContext;
/**
* Abstract {@link HttpAsyncResponseConsumer} implementation that relieves its
- * subclasses form having to synchronize access to internal instance variables
- * and provides a number of protected methods that they need to implement.
+ * subclasses from having to manage internal state and provides a number of protected
+ * event methods that they need to implement.
*
* @since 4.2
*/
-@ThreadSafe
public abstract class AbstractAsyncResponseConsumer<T> implements HttpAsyncResponseConsumer<T> {
- private volatile boolean completed;
+ private final AtomicBoolean completed;
+
private volatile T result;
private volatile Exception ex;
public AbstractAsyncResponseConsumer() {
super();
+ this.completed = new AtomicBoolean(false);
}
/**
@@ -120,7 +121,7 @@ public abstract class AbstractAsyncResponseConsumer<T> implements HttpAsyncRespo
/**
* Use {@link #onResponseReceived(HttpResponse)} instead.
*/
- public final synchronized void responseReceived(
+ public final void responseReceived(
final HttpResponse response) throws IOException, HttpException {
onResponseReceived(response);
final HttpEntity entity = response.getEntity();
@@ -133,7 +134,7 @@ public abstract class AbstractAsyncResponseConsumer<T> implements HttpAsyncRespo
/**
* Use {@link #onContentReceived(ContentDecoder, IOControl)} instead.
*/
- public final synchronized void consumeContent(
+ public final void consumeContent(
final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
onContentReceived(decoder, ioctrl);
}
@@ -141,45 +142,38 @@ public abstract class AbstractAsyncResponseConsumer<T> implements HttpAsyncRespo
/**
* Use {@link #buildResult(HttpContext)} instead.
*/
- public final synchronized void responseCompleted(final HttpContext context) {
- if (this.completed) {
- return;
- }
- this.completed = true;
- try {
- this.result = buildResult(context);
- } catch (final Exception ex) {
- this.ex = ex;
- } finally {
- releaseResources();
+ public final void responseCompleted(final HttpContext context) {
+ if (this.completed.compareAndSet(false, true)) {
+ try {
+ this.result = buildResult(context);
+ } catch (final Exception ex) {
+ this.ex = ex;
+ } finally {
+ releaseResources();
+ }
}
}
- public final synchronized boolean cancel() {
- if (this.completed) {
- return false;
+ public final boolean cancel() {
+ if (this.completed.compareAndSet(false, true)) {
+ releaseResources();
+ return true;
}
- this.completed = true;
- releaseResources();
- return true;
+ return false;
}
- public final synchronized void failed(final Exception ex) {
- if (this.completed) {
- return;
+ public final void failed(final Exception ex) {
+ if (this.completed.compareAndSet(false, true)) {
+ this.ex = ex;
+ releaseResources();
}
- this.completed = true;
- this.ex = ex;
- releaseResources();
}
- public final synchronized void close() throws IOException {
- if (this.completed) {
- return;
+ public final void close() throws IOException {
+ if (this.completed.compareAndSet(false, true)) {
+ releaseResources();
+ onClose();
}
- this.completed = true;
- releaseResources();
- onClose();
}
public Exception getException() {
@@ -191,7 +185,7 @@ public abstract class AbstractAsyncResponseConsumer<T> implements HttpAsyncRespo
}
public boolean isDone() {
- return this.completed;
+ return this.completed.get();
}
}