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:54 UTC
svn commit: r1631091 - in
/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol:
AbstractAsyncRequestConsumer.java AbstractAsyncResponseConsumer.java
BasicAsyncRequestConsumer.java BasicAsyncResponseConsumer.java
Author: olegk
Date: Sat Oct 11 17:46:53 2014
New Revision: 1631091
URL: http://svn.apache.org/r1631091
Log:
Use atomic boolean instead of synchronized access for state management in AbstractAsyncRequestConsumer / AbstractAsyncResponseConsumer
Modified:
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java?rev=1631091&r1=1631090&r2=1631091&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncRequestConsumer.java Sat Oct 11 17:46:53 2014
@@ -27,12 +27,12 @@
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;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
-import org.apache.http.annotation.ThreadSafe;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.IOControl;
@@ -40,20 +40,21 @@ 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);
}
/**
@@ -123,7 +124,7 @@ public abstract class AbstractAsyncReque
* Use {@link #onRequestReceived(HttpRequest)} instead.
*/
@Override
- public final synchronized void requestReceived(
+ public final void requestReceived(
final HttpRequest request) throws HttpException, IOException {
onRequestReceived(request);
if (request instanceof HttpEntityEnclosingRequest) {
@@ -139,7 +140,7 @@ public abstract class AbstractAsyncReque
* Use {@link #onContentReceived(ContentDecoder, IOControl)} instead.
*/
@Override
- public final synchronized void consumeContent(
+ public final void consumeContent(
final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
onContentReceived(decoder, ioctrl);
}
@@ -148,38 +149,32 @@ public abstract class AbstractAsyncReque
* Use {@link #buildResult(HttpContext)} instead.
*/
@Override
- 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();
+ }
}
}
@Override
- 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();
}
@Override
- 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();
}
@Override
@@ -194,7 +189,7 @@ public abstract class AbstractAsyncReque
@Override
public boolean isDone() {
- return this.completed;
+ return this.completed.get();
}
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java?rev=1631091&r1=1631090&r2=1631091&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/AbstractAsyncResponseConsumer.java Sat Oct 11 17:46:53 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);
}
/**
@@ -121,7 +122,7 @@ public abstract class AbstractAsyncRespo
* Use {@link #onResponseReceived(HttpResponse)} instead.
*/
@Override
- public final synchronized void responseReceived(
+ public final void responseReceived(
final HttpResponse response) throws IOException, HttpException {
onResponseReceived(response);
final HttpEntity entity = response.getEntity();
@@ -135,7 +136,7 @@ public abstract class AbstractAsyncRespo
* Use {@link #onContentReceived(ContentDecoder, IOControl)} instead.
*/
@Override
- public final synchronized void consumeContent(
+ public final void consumeContent(
final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
onContentReceived(decoder, ioctrl);
}
@@ -144,48 +145,41 @@ public abstract class AbstractAsyncRespo
* Use {@link #buildResult(HttpContext)} instead.
*/
@Override
- 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();
+ }
}
}
@Override
- 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;
}
@Override
- 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();
}
@Override
- 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();
}
@Override
@@ -200,7 +194,7 @@ public abstract class AbstractAsyncRespo
@Override
public boolean isDone() {
- return this.completed;
+ return this.completed.get();
}
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java?rev=1631091&r1=1631090&r2=1631091&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java Sat Oct 11 17:46:53 2014
@@ -32,7 +32,6 @@ import org.apache.http.ContentTooLongExc
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
-import org.apache.http.annotation.ThreadSafe;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.IOControl;
@@ -49,7 +48,6 @@ import org.apache.http.util.Asserts;
*
* @since 4.2
*/
-@ThreadSafe
public class BasicAsyncRequestConsumer extends AbstractAsyncRequestConsumer<HttpRequest> {
private volatile HttpRequest request;
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java?rev=1631091&r1=1631090&r2=1631091&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java Sat Oct 11 17:46:53 2014
@@ -31,7 +31,6 @@ import java.io.IOException;
import org.apache.http.ContentTooLongException;
import org.apache.http.HttpEntity;
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;
@@ -48,7 +47,6 @@ import org.apache.http.util.Asserts;
*
* @since 4.2
*/
-@ThreadSafe
public class BasicAsyncResponseConsumer extends AbstractAsyncResponseConsumer<HttpResponse> {
private volatile HttpResponse response;