Skip to content

Commit 31b22c2

Browse files
committed
small changes
1 parent 765b80a commit 31b22c2

2 files changed

Lines changed: 22 additions & 17 deletions

File tree

httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Async.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@
3131
import java.util.concurrent.Future;
3232
import java.util.concurrent.LinkedBlockingQueue;
3333
import java.util.concurrent.RejectedExecutionException;
34-
import java.util.concurrent.ThreadFactory;
3534
import java.util.concurrent.ThreadPoolExecutor;
3635
import java.util.concurrent.TimeUnit;
3736
import java.util.concurrent.atomic.AtomicInteger;
3837

38+
import org.apache.hc.core5.annotation.Contract;
39+
import org.apache.hc.core5.annotation.ThreadingBehavior;
3940
import org.apache.hc.core5.concurrent.BasicFuture;
41+
import org.apache.hc.core5.concurrent.DefaultThreadFactory;
4042
import org.apache.hc.core5.concurrent.FutureCallback;
4143
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
4244
import org.apache.hc.core5.util.Args;
@@ -46,6 +48,7 @@
4648
*
4749
* @since 4.3
4850
*/
51+
@Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
4952
public class Async {
5053

5154
private static final int DEFAULT_MAX_THREADS =
@@ -56,8 +59,8 @@ public class Async {
5659
private static final AtomicInteger INSTANCE_COUNT = new AtomicInteger(0);
5760

5861
private Executor executor;
59-
private java.util.concurrent.Executor concurrentExec;
60-
private ExecutorService ownedConcurrentExec;
62+
private volatile java.util.concurrent.Executor concurrentExec;
63+
private volatile ExecutorService ownedConcurrentExec;
6164

6265
private int maxThreads = DEFAULT_MAX_THREADS;
6366
private int queueCapacity = DEFAULT_QUEUE_CAPACITY;
@@ -85,13 +88,23 @@ public Async queueCapacity(final int queueCapacity) {
8588
return this;
8689
}
8790

91+
/**
92+
* Enables an owned bounded default executor for asynchronous request execution using the
93+
* current {@code maxThreads} and {@code queueCapacity} settings.
94+
*
95+
* @return this instance.
96+
* @since 5.7
97+
*/
98+
public Async useDefaultExecutor() {
99+
return useDefaultExecutor(this.maxThreads, this.queueCapacity);
100+
}
101+
88102
/**
89103
* Enables an owned bounded default executor for asynchronous request execution.
90104
*
91-
* @param maxThreads maximum number of threads.
105+
* @param maxThreads maximum number of threads.
92106
* @param queueCapacity maximum number of queued tasks.
93107
* @return this instance.
94-
*
95108
* @since 5.7
96109
*/
97110
public Async useDefaultExecutor(final int maxThreads, final int queueCapacity) {
@@ -116,13 +129,9 @@ private void rebuildOwnedExecutorIfActive() {
116129

117130
private static ExecutorService createDefaultExecutor(final int maxThreads, final int queueCapacity) {
118131
final int instanceId = INSTANCE_COUNT.incrementAndGet();
119-
final AtomicInteger threadCount = new AtomicInteger(0);
120-
121-
final ThreadFactory threadFactory = r -> {
122-
final Thread t = new Thread(r, "httpclient5-fluent-async-" + instanceId + "-" + threadCount.incrementAndGet());
123-
t.setDaemon(true);
124-
return t;
125-
};
132+
final DefaultThreadFactory threadFactory = new DefaultThreadFactory(
133+
"httpclient5-fluent-async-" + instanceId + "-",
134+
true);
126135

127136
final ThreadPoolExecutor exec = new ThreadPoolExecutor(
128137
maxThreads,
@@ -239,7 +248,6 @@ public Future<Content> execute(final Request request) {
239248
* @param handler the response handler.
240249
* @param <T> the handler result type.
241250
* @return a {@code CompletableFuture} producing the handler result.
242-
*
243251
* @since 5.7
244252
*/
245253
public <T> CompletableFuture<T> executeAsync(final Request request, final HttpClientResponseHandler<T> handler) {
@@ -275,7 +283,6 @@ public void cancelled() {
275283
* @param callback the callback to invoke on completion, failure, or cancellation; may be {@code null}.
276284
* @param <T> the handler result type.
277285
* @return a {@code CompletableFuture} producing the handler result.
278-
*
279286
* @since 5.7
280287
*/
281288
public <T> CompletableFuture<T> executeAsync(
@@ -317,7 +324,6 @@ public void cancelled() {
317324
*
318325
* @param request the request to execute.
319326
* @return a {@code CompletableFuture} producing the response {@code Content}.
320-
*
321327
* @since 5.7
322328
*/
323329
public CompletableFuture<Content> executeAsync(final Request request) {
@@ -332,7 +338,6 @@ public CompletableFuture<Content> executeAsync(final Request request) {
332338
* @param request the request to execute.
333339
* @param callback the callback to invoke on completion, failure, or cancellation; may be {@code null}.
334340
* @return a {@code CompletableFuture} producing the response {@code Content}.
335-
*
336341
* @since 5.7
337342
*/
338343
public CompletableFuture<Content> executeAsync(final Request request, final FutureCallback<Content> callback) {

httpclient5-fluent/src/test/java/org/apache/hc/client5/http/examples/fluent/FluentAsyncCompletableFutureCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static void main(final String... args) throws Exception {
5050
Request.get("http://www.apple.com/")
5151
);
5252

53-
final Async async = Async.newInstance().useDefaultExecutor(8, 500);
53+
final Async async = Async.newInstance().maxThreads(8).queueCapacity(500).useDefaultExecutor();
5454
try {
5555

5656
final CompletableFuture<?>[] futures = requests.stream()

0 commit comments

Comments
 (0)