Skip to main content

All Questions

Filter by
Sorted by
Tagged with
0 votes
1 answer
70 views

What problems can I have with spawning a set of new threads from Servlet and what alternatives exist?

For simplicity, I will omit some details and try to highlight the main point: I have a servlet that consumes users' requests, when a user sends a request I need to gather data from many endpoints. One ...
Pavel Petrashov's user avatar
-1 votes
1 answer
102 views

Java Unexpected Async Behavior

Im expecting unexpected behaviour in Java(Java 17, Spring Boot app). This method is for test and Im calling it directly from the main method. Shouldnt the main thread get blocked at the line of ...
Ivan M's user avatar
  • 1
1 vote
1 answer
274 views

How to use CompletableFuture to execute the threads parallaly without waiting and combine the result?

I have executeGetCapability method which is executed in different threads but these threads runs sequentially..meaning one is completed after the other @Async("threadPoolCapabilitiesExecutor&...
MiGo's user avatar
  • 763
1 vote
1 answer
6k views

How does whenComplete() work in a chain of CompletionStages?

I thought I understood whenComplete but I'm not sure now. This question originated in another thread. The way we work with futures in my company is by chaining them: CompletionStage<Foo> getFoo()...
Andrew Cheong's user avatar
1 vote
0 answers
299 views

Pass transaction context under completableFutures

I have a code that basically does something like this: private void example() { DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setName("name"); ...
Erick De Miranda Oliveira's user avatar
0 votes
0 answers
43 views

Java: Halt method execution after a certain time

Say I have a method called doSomething, and I call the method like this: int result = doSomething(); And suppose doSomething() takes 500ms to execute by default. However, I want to halt the execution ...
swing1234's user avatar
  • 233
0 votes
1 answer
654 views

CompletableFuture.runAsync vs array of CompletableFuture

I found this code in a project: int threadCount = 10; CompletableFuture<?>[] futures = new CompletableFuture<?>[threadCount]; for (int i = 0; i < threadCount; i++) { futures[i] = ...
akuma8's user avatar
  • 4,683
9 votes
1 answer
9k views

Collect thrown exceptions from CompletableFuture.allOf() execution

Having following scratch code: public static void main(String[] args) throws ExecutionException, InterruptedException { CompletableFuture<Void> process1 = CompletableFuture.runAsync(() -&...
Kamil W's user avatar
  • 2,375
1 vote
2 answers
2k views

How to perform resource cleanup for CompletableFuture in Java?

I've a piece of code in CompletableFuture that performs retry if there are exceptions else completes the task. I've passed a resource to the Supplier and Consumer to perform the task and want to close ...
Lother's user avatar
  • 43
3 votes
2 answers
2k views

Calling ExecutorService.shutdownNow from CompletableFuture

I need to cancel all scheduled but not yet running CompletableFuture tasks when one of already running task throws an exception. Tried following example but most of the time the main method does not ...
tsolakp's user avatar
  • 5,948
0 votes
2 answers
67 views

Optimize async operation executed by rest for many users

in following code I have asynchronous method which is executed by rest by authenticated user. In this method I execute loop in which is checking periodically cache of new data. @Async public ...
Denis Stephanov's user avatar
2 votes
3 answers
13k views

CompletableFuture: several tasks

How can I asynchronously execute 20 Runnable tasks(or 1 task 20 times), using 5 CompletableFutures? That's what I've got: Runnable task = () -> { long startTime = System.currentTimeMillis(...
Nick Kulese's user avatar
3 votes
1 answer
924 views

Situational use: Run tasks in ForkJoinPool vs. new Thread

Since I'm trying to understand a few of the new features provided in Java 8, I came upon the following situation: I wish to implement asynchron method calls into my application (in JavaFX). The idea ...
CRoemheld's user avatar
  • 947
0 votes
3 answers
354 views

Using the faster output from 2 threads

I want to work with two threads in my Java program for a tiny part. I need to give the first call to a database and the second call to an API, both calls with same input, and then work with the output ...
Padfoot's user avatar
  • 11
7 votes
1 answer
3k views

How do I use CompletableFuture.supplyAsync together with PriorityBlockingQueue?

I'm trying to add a priority queue to an existing application that uses ThreadPoolExecutor with a LinkedBlockingQueue via CompletableFuture.supplyAsync. The problem is that I can't come up with a ...
jacob's user avatar
  • 2,906