Java 8 CompletableFuture future is waiting needlessly for other futures - java

I have this simple code below. All futures should start at the same time. future13 is supposed to run right after futures 1 and 3 finish, but in the logs I see that it waits until after futures 1, 2, 3, and 4 all finish. Why does it wait for futures 2 and 4?
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.junit.Test;
public class Test1 {
private void loop(Long id, int max) {
try {
for (int i = 0; i < max; i++) {
System.out.println(id);
Thread.sleep(100);
}
} catch (Throwable t) {
System.out.println(t);
}
}
private CompletableFuture<Void> createConfigFuture(Long id) {
return CompletableFuture.supplyAsync(() -> {
loop(id, 100);
return null;
});
}
#Test
public void testMe() {
CompletableFuture<Void> future1 = createConfigFuture(1L);
CompletableFuture<Void> future2 = createConfigFuture(2L);
CompletableFuture<Void> future3 = createConfigFuture(3L);
CompletableFuture<Void> future4 = createConfigFuture(4L);
try {
CompletableFuture<Void> future13 = CompletableFuture.allOf(future1, future3)
.thenApply(v -> {
loop(999L, 5);
return null;
});
CompletableFuture<Void> mainFuture = CompletableFuture.allOf(future13, future2, future4);
mainFuture.get();
} catch (InterruptedException | ExecutionException e) {
System.out.println(e);
}
}
}

There is a queue to get an execution slot in the JRE's default fork-join Executor which all async tasks will serialize on.
The task #2 is ahead of the task #3 in that Executor queue, so before you observe the execution of the task #3 (and, respectively, of the completion task #13) the #2 should get its execution slot first.
This may be seen as #3 linked to #2, but other than that there should not be any additional coupling between the tasks.

Related

What is the prod and cons of following implementation for waiting before execution when thread queue is full? [duplicate]

This question already has answers here:
ThreadPoolExecutor Block When its Queue Is Full?
(10 answers)
Closed 3 months ago.
We have a large text file in which each line requires intensive process. The design is to have a class that reads the file and delegates the processing of each line to a thread, via thread pool. The file reader class should be blocked from reading the next line once there is no free thread in the pool to do the processing. So i need a blocking thread pool
In the current implementation ThreadPoolExecutor.submit() and ThreadPoolExecutor.execute() methods throw RejectedExecutionException exception after the configured # of threads get busy as i showed in code snippet below.
public class BlockingTp {
public static void main(String[] args) {
BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
ThreadPoolExecutor executorService=
new ThreadPoolExecutor(1, 3, 30, TimeUnit.SECONDS, blockingQueue);
int Jobs = 10;
System.out.println("Starting application with " + Jobs + " jobs");
for (int i = 1; i <= Jobs; i++)
try {
executorService.submit(new WorkerThread(i));
System.out.println("job added " + (i));
} catch (RejectedExecutionException e) {
System.err.println("RejectedExecutionException");
}
}
}
class WorkerThread implements Runnable {
int job;
public WorkerThread(int job) {
this.job = job;
}
public void run() {
try {
Thread.sleep(1000);
} catch (Exception excep) {
}
}
}
Output of above program is
Starting application to add 10 jobs
Added job #1
Added job #2
Added job #3
Added job #4
Added job #5
Added job #6
RejectedExecutionException
RejectedExecutionException
RejectedExecutionException
RejectedExecutionException
Can some one throw some light i.e how i can implement blocking thread pool.
Can some one throw some light i.e how i can implement blocking thread pool.
You need to set a rejection execution handler on your executor service. When the thread goes to put the job into the executor, it will block until there is space in the blocking queue.
BlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
ThreadPoolExecutor executorService =
new ThreadPoolExecutor(1, 3, 30, TimeUnit.SECONDS, arrayBlockingQueue);
// when the blocking queue is full, this tries to put into the queue which blocks
executorService.setRejectedExecutionHandler(new RejectedExecutionHandler() {
#Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
try {
// block until there's room
executor.getQueue().put(r);
// check afterwards and throw if pool shutdown
if (executor.isShutdown()) {
throw new RejectedExecutionException(
"Task " + r + " rejected from " + executor);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RejectedExecutionException("Producer interrupted", e);
}
}
});
So instead of the TRE throwing a RejectedExecutionException, it will call the rejection handler which will in turn try to put the job back on the queue. This blocks the caller.
Lets have a look at your code again:
for (int i = 1; i <= Jobs; i++)
try {
tpExe.submit(new WorkerThread(i));
System.out.println("job added " + (i));
} catch (RejectedExecutionException e) {
System.err.println("RejectedExecutionException");
}
So - when you try to submit, and the pool is busy, that exception is thrown. If you want to wrap around that, it could look like:
public void yourSubmit(Runnable whatever) {
boolean submitted = false;
while (! submitted ) {
try {
tpExe.submit(new WorkerThread(whatever));
submitted = true;
} catch (RejectedExecutionException re) {
// all threads busy ... so wait some time
Thread.sleep(1000);
}
In other words: use that exception as "marker" that submits are currently not possible.
You can use semaphore for to control the resource.Reader will read and create asynchronous task by acquiring semaphore.If every thread is busy the reader thread will wait till thread is available.
public class MyExecutor {
private final Executor exec;
private final Semaphore semaphore;
public BoundedExecutor(Executor exec, int bound) {
this.exec = exec;
this.semaphore = new Semaphore(bound);
}
public void submitTask(final Runnable command)
throws InterruptedException, RejectedExecutionException {
semaphore.acquire();
try {
exec.execute(new Runnable() {
public void run() {
try {
command.run();
} finally {
semaphore.release();
}
}
});
} catch (RejectedExecutionException e) {
semaphore.release();
throw e;
}
}
}
Here is a RejectedExecutionHandler that supports the desired behavior. Unlike other implementations, it does not interact with the queue directly so it should be compatible with all Executor implementations and will not deadlock.
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.BiFunction;
import static com.github.cowwoc.requirements.DefaultRequirements.assertThat;
import static com.github.cowwoc.requirements.DefaultRequirements.requireThat;
/**
* Applies a different rejection policy depending on the thread that requested execution.
*/
public final class ThreadDependantRejectionHandler implements RejectedExecutionHandler
{
private final ThreadLocal<Integer> numberOfRejections = ThreadLocal.withInitial(() -> 0);
private final BiFunction<Thread, Executor, Action> threadToAction;
/**
* #param threadToAction indicates what action a thread should take when execution is rejected
* #throws NullPointerException if {#code threadToAction} is null
*/
public ThreadDependantRejectionHandler(BiFunction<Thread, Executor, Action> threadToAction)
{
requireThat(threadToAction, "threadToAction").isNotNull();
this.threadToAction = threadToAction;
}
#SuppressWarnings("BusyWait")
#Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor)
{
if (executor.isShutdown())
return;
Thread currentThread = Thread.currentThread();
Action action = threadToAction.apply(currentThread, executor);
if (action == Action.RUN)
{
r.run();
return;
}
if (action == Action.REJECT)
{
throw new RejectedExecutionException("The thread pool queue is full and the current thread is not " +
"allowed to block or run the task");
}
assertThat(action, "action").isEqualTo(Action.BLOCK);
int numberOfRejections = this.numberOfRejections.get();
++numberOfRejections;
this.numberOfRejections.set(numberOfRejections);
if (numberOfRejections > 1)
return;
try
{
ThreadLocalRandom random = ThreadLocalRandom.current();
while (!executor.isShutdown())
{
try
{
Thread.sleep(random.nextInt(10, 1001));
}
catch (InterruptedException e)
{
throw new WrappingException(e);
}
executor.submit(r);
numberOfRejections = this.numberOfRejections.get();
if (numberOfRejections == 1)
{
// Task was accepted, or executor has shut down
return;
}
// Task was rejected, reset the counter and try again.
numberOfRejections = 1;
this.numberOfRejections.set(numberOfRejections);
}
throw new RejectedExecutionException("Task " + r + " rejected from " + executor + " because " +
"the executor has been shut down");
}
finally
{
this.numberOfRejections.set(0);
}
}
public enum Action
{
/**
* The thread should run the task directly instead of waiting for the executor.
*/
RUN,
/**
* The thread should block until the executor is ready to run the task.
*/
BLOCK,
/**
* The thread should reject execution of the task.
*/
REJECT
}
}
This works for me.
class handler implements RejectedExecutionHandler{
#Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
try {
executor.getQueue().put(r);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

When using a thread pool, call Future#get and the program hangs

I created a thread pool, and submitted two tasks. Why does my application hang without any exceptions after print task one ,result: null???
private final static ThreadPoolExecutor executorService = new
ThreadPoolExecutor(1, 1, 1L, TimeUnit.MINUTES,
new SynchronousQueue<Runnable>(), new ThreadPoolExecutor.DiscardPolicy());
public static void main(String[] args) throws Exception {
Future taskOne = executorService.submit(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Future taskTwo = executorService.submit(() -> System.out.println("task two is working"));;
System.out.println("task one ,result: " + taskOne.get());
System.out.println("task two, result: " + taskTwo.get());
executorService.shutdown();
}
When you submit the second task, the rejection policy is triggered because the thread pool uses SynchronousQueue and maximumPoolSize is 1, while the first task has not completed. You are using DiscardPolicy, which means that the thread pool does nothing and returns you a FutureTask whose state is always NEW.
public static class DiscardPolicy implements RejectedExecutionHandler {
public DiscardPolicy() { }
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
So when you call taskTwo#get(), you will always be blocked. (FutureTask will always be blocked when it is in a state smaller than COMPLETING, see FutureTask#get).
You can use AbortPolicy (the default policy), so that when you execute executorService.submit(() - > submit; System.out.println("task two is working")), you immediately get a RejectedExecutionException.
Or use Future#get(timeout), in which case you get a TimeoutException if you do not get a result for a specified time.
new ThreadPoolExecutor.DiscardPolicy() silently discards the new task when it fails to submit it. here taskTwo wants to get executed, it never gets a chance to execute.
DiscardPolicy() method internally call void rejectedExecution(Runnable r, ThreadPoolExecutor executor) from RejectedExecutionHandler interface.
I have shown CustomRejectedExecutionHandler for better understanding the taskTwo thread condition. As taskTwo is silently discarded so that taskTwo.get() method will never be able to return data.
That's why timeout is required to be set as 1 second (taskTwo.get(1000, TimeUnit.MILLISECONDS)).
package example;
import java.util.concurrent.*;
public class ThreadPoolEx {
public static void main(String[] args) {
CustomRejectedExecutionHandler rejectionHandler = new CustomRejectedExecutionHandler();
ThreadPoolExecutor executorService =
new ThreadPoolExecutor(1, 1, 1L,
TimeUnit.MINUTES,
new SynchronousQueue<Runnable>(),
rejectionHandler
);
Future taskOne = executorService.submit(() -> {
try {
System.out.println("taskOne is going to sleep");
Thread.sleep(2000);
System.out.println("taskOne is wake up");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Future taskTwo = executorService.submit(() -> System.out.println("task two is working"));
try {
System.out.println("task one ,result: " + taskOne.get());
System.out.println("isTerminating "+ executorService.isTerminating());
System.out.println("getActiveCount "+ executorService.getActiveCount());
System.out.println("is cancelled " + taskTwo.isCancelled());
System.out.println("is isDone " + taskTwo.isDone());
System.out.println("task two, result: " + taskTwo.get(1000, TimeUnit.MILLISECONDS));
} catch (Exception e) {
}
executorService.shutdown();
}
}
class CustomRejectedExecutionHandler implements RejectedExecutionHandler {
#Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println(r.toString() + " is rejected");
}
}

java8 asynchronous method CompletableFuture.runAsync doesn't run

very basic code to run asynchronous method.
when I run the following code the runAsync doesn't run.
what I'm missing?
the result run only the sync code .
public class Main {
public static void main(String[] args) {
runAsync("run async command ans wait 10000");
System.out.println("sync commands ");
}
public static void runAsync(String inputStr) {
CompletableFuture.runAsync(() -> {
List<String> strings = Arrays.asList(inputStr.split(" "));
int sleep = Integer.parseInt(strings.get(strings.size() - 1));
try {
sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("async command ");
});
}
}
I expect to get first the "sync commands" then the "async command "
but get only the sync message
Your task will run in some other Thread(by default in a Thread from ForkJoinPool) and you are not waiting for it to finish - the main Thread ends before your async task is executed/submitted. You can call CompletableFuture::join to wait for it to finish and it will block the main Thread until it finishes :
CompletableFuture.runAsync(() -> {
List<String> strings = Arrays.asList(inputStr.split(" "));
int sleep = Integer.parseInt(strings.get(strings.size() - 1));
try {
sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("async command ");
}).join(); //here call the join
or like :
CompletableFuture<Void> cf = CompletableFuture.runAsync(() -> {
//...
});
cf.join();
You need to wait for the async task to be completed by using join, for example:
public static void main(String[] args) {
CompletableFuture<Void> future = runAsync("run async command ans wait 10000");
future.join();
System.out.println("sync commands ");
}
public static CompletableFuture<Void> runAsync(String inputStr) {
return CompletableFuture.runAsync(() -> {
List<String> strings = Arrays.asList(inputStr.split(" "));
int sleep = Integer.parseInt(strings.get(strings.size() - 1));
try {
sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("async command ");
});
}
It does run, but it runs on another thread and you're not waiting or doing anything with the result. As Javadoc of CompletableFuture.runAsync() says:
Returns a new CompletableFuture that is asynchronously completed by a
task running in the ForkJoinPool.commonPool() after it runs the given
action.
runAsync() is useful for tasks that don't return anything. If you want a result from it you should use supplyAsync() which returns a CompletableFuture<T>
Then you can get the result from it:
// Run a task specified by a Supplier object asynchronously
CompletableFuture<String> future = CompletableFuture.supplyAsync(new Supplier<String>() {
#Override
public String get() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return "Result of the asynchronous computation";
}
});
// Block and get the result of the Future
String result = future.get();
System.out.println(result);

Execute multiple java methods asynchronously and get the results on job done

how to execute multiple java methods asynchronously and get the results on job done for each method?
Lets say I have this:
for(int i=1; i<=10000; i++) {
kur(i);
}
//on kur(i) finish -> System.out.println(kur(i)) or System.out.println(Exception e)
Info kur(int i) throws Exception {
//do some stuff
return new Info(...);
}
I can use spring boot also. I checked this: https://spring.io/guides/gs/async-method/ but it is different than my case.
Could you help me?
(ORIGINAL ANSWER) Maybe an ExecutorService could help you?
ExecutorService executorService = Executors.newFixedThreadPool(10);
IntStream.rangeClosed(1, 10000)
.forEach(i -> {
executorService.submit(() -> {
try {
System.out.println(kur(i));
} catch (Exception e) {
// do something useful here - remember you're in a separate thread
//
// this is not useful.
e.printStackTrace();
}
});
});
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
(EDIT WITH POOR MAN'S SOLUTION TO WAIT FOR EVERYTHING TO COMPLETE):
ExecutorService executorService = Executors.newFixedThreadPool(10);
List<Future<?>> futures = IntStream.rangeClosed(1, 10000)
.mapToObj(i -> {
return executorService.submit(() -> {
try {
System.out.println(kur(i));
} catch (Exception e) {
// do something useful here - remember you're in a separate thread
//
// this is not useful.
e.printStackTrace();
}
});
})
.collect(Collectors.toList());
for (Future<?> f : futures) {
f.get();
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);

ThreadPoolExecutor: cancel tasks from invokeAll() when one task returns an error

I have a threadpool executor doing the same operation for a list of keys coming in batches. so i am using invokeall() method to do the processing for a list of keys in a batch. the usecase is such that if any of the tasks in a batch returns an error, there is no point to continue processing for other keys. So
how can i cancel the tasks of the batch execution once a task has retuned an error.
but not effect the other batch of keys execution. ie the cancellation should be isolated per batch.
Thanks for your help.
I don't see how this can be done without a bit of customization. The simplest implementation I could come up with requires:
a specialized Future implementation basically a subclass of FutureTask which overrides the setException() method in order to cancel all other tasks when a task throws an exception
a specialized ThreadPoolExecutor implementation which overrides the invokeAll() to make use of the custom future
It goes like this:
for the custom future:
import java.util.Collection;
import java.util.concurrent.*;
public class MyFutureTask<V> extends FutureTask<V> {
private Callable<V> task;
private Collection<Future<V>> allFutures;
public MyFutureTask(Callable<V> task, Collection<Future<V>> allFutures) {
super(task);
this.task = task;
this.allFutures = allFutures;
}
#Override
protected void setException(Throwable t) {
super.setException(t);
synchronized(allFutures) {
for (Future<V> future: allFutures) {
if ((future != this) && !future.isDone()) {
future.cancel(true);
}
}
}
}
}
for the custom thread pool:
import java.util.*;
import java.util.concurrent.*;
public class MyThreadPool extends ThreadPoolExecutor {
public MyThreadPool(int size) {
super(size, size, 1L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
#Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException {
List<Future<T>> futures = new ArrayList<>(tasks.size());
for (Callable<T> callable: tasks) {
futures.add(new MyFutureTask<>(callable, futures));
}
for (Future<T> future: futures) {
execute((MyFutureTask<T>) future);
}
for (Future<T> future: futures) {
try {
future.get();
} catch (ExecutionException|CancellationException e) {
// ignore this exception
}
}
return futures;
}
}
code example to test it:
import java.util.*;
import java.util.concurrent.*;
public class TestThreadPool {
public static void main(final String[] args) {
ExecutorService executor = null;
try {
int size = 10;
executor = new MyThreadPool(size);
List<Callable<String>> tasks = new ArrayList<>();
int count=1;
tasks.add(new MyCallable(count++, false));
tasks.add(new MyCallable(count++, true));
List<Future<String>> futures = executor.invokeAll(tasks);
System.out.println("results:");
for (int i=0; i<futures.size(); i++) {
Future<String> f = futures.get(i);
try {
System.out.println(f.get());
} catch (CancellationException e) {
System.out.println("CancellationException for task " + (i+1) +
": " + e.getMessage());
} catch (ExecutionException e) {
System.out.println("ExecutionException for task " + (i+1) +
": " + e.getMessage());
}
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if (executor != null) executor.shutdownNow();
}
}
public static class MyCallable implements Callable<String> {
private final int index;
private final boolean simulateFailure;
public MyCallable(int index, boolean simulateFailure) {
this.index = index;
this.simulateFailure = simulateFailure;
}
#Override
public String call() throws Exception {
if (simulateFailure) {
throw new Exception("task " + index + " simulated failure");
}
Thread.sleep(2000L);
return "task " + index + " succesful";
}
}
}
and finally the outcome of executing the test, as displayed in the output console:
results:
CancellationException for task 1: null
ExecutionException for task 2: java.lang.Exception: task 2 simulated failure
Pass the reference of the ExecutorService to each task as bellow:
ExecutorService eServ = Executors.newFixedThreadPool(10);
Set<Callable<ReaderThread>> tasks = new HashSet<Callable<ReaderThread>>();
for (int i = 0; i < 10 ; i++)
{
tasks.add(new ReaderThread(eServ));
}
List<Future<ReaderThread>> lt = eServ.invokeAll(tasks);
If task is error then call the shutdownNow() then it will stop all the tasks
public ReaderThread call() throws Exception
{
try
{
for (int i = 1; i < 50; i++)
{
System.out.println("i="+i+"::"+Thread.currentThread());
Thread.sleep(1000);
if (i == 10 && Thread.currentThread().toString().equals("Thread[pool-1-thread-7,5,main]"))
{
throw new Exception();
}
}
}
catch ( Exception exc)
{
ex.shutdownNow();
}
return this;
}

Categories