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 2014/10/11 19:46:24 UTC
svn commit: r1631089 - in
/httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol:
AbstractAsyncRequestConsumer.java AbstractAsyncResponseConsumer.java
Author: olegk
Date: Sat Oct 11 17:46:24 2014
New Revision: 1631089
URL: http://svn.apache.org/r1631089
Log:
Use atomic boolean instead of synchronized access for state management in AbstractAsyncRequestConsumer / AbstractAsyncResponseConsumer
Modified:
httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java
httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java
Modified: httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java?rev=1631089&r1=1631088&r2=1631089&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java (original)
+++ httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java Sat Oct 11 17:46:24 2014
@@ -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.HttpCont
/**
* 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 AbstractAsyncReque
/**
* 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 AbstractAsyncReque
/**
* 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 AbstractAsyncReque
/**
* 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 AbstractAsyncReque
}
public boolean isDone() {
- return this.completed;
+ return this.completed.get();
}
}
Modified: httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java?rev=1631089&r1=1631088&r2=1631089&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java (original)
+++ httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java Sat Oct 11 17:46:24 2014
@@ -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.HttpCont
/**
* 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 AbstractAsyncRespo
/**
* 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 AbstractAsyncRespo
/**
* 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 AbstractAsyncRespo
/**
* 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 AbstractAsyncRespo
}
public boolean isDone() {
- return this.completed;
+ return this.completed.get();
}
}