Java concurrent programming - endless loop - java

I am reading the book Introducing Play Framework: Java Web Application Development (ISBN 978-1-4842-5645-9) and there is this example on Callable:
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CallableClient {
/**
* #param args
*/
// Step1 : Create a Runnable
public static void main(String[] args) {
Callable callableTask = new CallableTask();
// Step 2: Configure Executor
// Uses FixedThreadPool executor
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<String> future = executor.submit(callableTask);
boolean listen = true;
while (listen) {
if (future.isDone()) {
String result;
try {
result = future.get();
listen = false;
System.out.println(result);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
executor.shutdown();
}
}
My question is, if the computation of the Future throws and exception, will the while loop run forever?
In my opinion, yes, it will loop forever.
First, if the Future computation throws an exception, then
future.isDone()
always evaluates to true. So we reach the inside of the if, where the stopping condition can be set. Still Ok.
Second, as per future documentation, when reaching line
result = future.get();
it will always throw ExecutionException because the computation threw an exception. This exception is caught in the try-catch block, without reaching the stopping condition precondition, which is
listen = false
Lastly, the above will create an endless cycle.
Is there any mistake in my assumptions or is the author of the example really wrong?

The program mentioned above cycles forever, if the callable throws an exception.
This is a code snippet with the callable that throws an exceptions. Executing compiled snippet loops forever.
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CallableClient {
public static class CallableTask implements Callable<String> {
#Override
public String call() throws Exception {
throw new Exception();
}
}
public static void main(String[] args) {
Callable<String> callableTask = new CallableTask();
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<String> future = executor.submit(callableTask);
boolean listen = true;
while (listen) {
if (future.isDone()) {
String result;
try {
result = future.get();
listen = false;
System.out.println(result);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
executor.shutdown();
}
}

Related

How ExecutorService works when submit a callable task with callback

How to add custom check in executorService.submit() callback.
I want my thread not to do anything, whenever flag is true. If flag is false then it should work as usual.
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class test{
private static volatile boolean flag= false;
public static void main(String[] args) throws ExecutionException, InterruptedException {
Callable<Boolean> callableTask = () -> {
return isFlag();
};
Future a = submitAsync(callableTask);
System.out.println("tartest"+a.get());
}
public static Future submitAsync(Callable taskToRun) {
ExecutorService e1 = Executors.newSingleThreadExecutor(new BasicThreadFactory.Builder().namingPattern("").build());
return e1.submit(() -> {
try {
if (flag) {
return;
}
taskToRun.call();
} catch (Exception e) {
System.out.println("garbage ---");
}
});
// return e1.submit(taskToRun);
}
public static boolean isFlag() {
return true;
}
}
Here, a.get() is returning null. If I replace e1.submit(....) with e1.submit(taskToRun), which is commented in given code snippet then its working fine. So why a.get() is null?
return e1.submit(() -> {
try {
if (flag) {
return;
}
taskToRun.call();
} catch (Exception e) {
System.out.println("garbage ---");
}
});
In this snippet, the lambda passed to submit method on ExecutorService e1 is interpreted as a Runnable and that is why the return value is null. Check this documentation for more details. You need to pass the callable taskToRun itself to e1.submit() to be able to get the return value of the callable. Also, it would be cleaner to put the logic inside the Callable itself.
I have rewritten the sample code to achieve your requirement.
package stackoverflow.test;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class test{
private static volatile boolean flag = true;
public static void main(String[] args) throws ExecutionException, InterruptedException {
Callable<Boolean> callableTask = () -> {
if (flag) {
System.out.println("Flag is true, returning without executing callable logic !");
return false;
}
System.out.println("Flag is false, executing callable logic !");
return isFlag();
};
Future a = submitAsync(callableTask);
System.out.println("test " + a.get());
}
private static Future<Boolean> submitAsync(Callable<Boolean> taskToRun) {
ExecutorService e1 = Executors.newSingleThreadExecutor(new BasicThreadFactory.Builder().namingPattern("").build());
return e1.submit(taskToRun);
}
private static boolean isFlag() {
return true;
}
}
Your submitAsync method returns a Future that is the result of calling ExecutorService.submit(Runnable). From the documentation of that method:
Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return null upon successful completion.
If you want to return a Future that yields a value, add return statements to your lambda, so it will be interpreted as a Callable instead of a Runnable:
return taskToRun.call();
} catch (Exception e) {
e.printStackTrace();
return null;
}

Java lock's condition implementation

Need help with code at below link as it should run indefinitely likewise with any typical producer/consumer problem but somehow it is getting stuck on call of condition.signal(). What am I doing wrong here?
In main method, I have created two thread, one is consumer and other one is producer. it has shared task queue where both updates the entry.
package com.anurgup.handson;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ConditionService implements Runnable {
Lock lock = new ReentrantLock();
Condition added = lock.newCondition();
Condition removed = lock.newCondition();
// type of service
String type;
// shared task for insertion and deletion of task
static Queue<Integer> task = new PriorityQueue<Integer>();
// max number of task allowed
private static final int MAX_SIZE = 5;
public ConditionService(String type) {
this.type = type;
}
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(2);
service.submit(new ConditionService("producer"));
service.submit(new ConditionService("consumer"));
}
public void produce() {
try {
while (true) {
System.out.println("in producer...");
synchronized (task) {
while (task.size() == MAX_SIZE)
removed.await();
System.out.println("added item: " + task.size());
task.add(task.size());
added.signal();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void consume() {
try {
while (true) {
System.out.println("in consumer...");
synchronized (task) {
while (task.isEmpty())
added.await();
System.out.println("removed item: " + task.peek());
task.remove();
removed.signal();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
#Override
public void run() {
if (this.type.equals("producer"))
this.produce();
else
this.consume();
}
}
You're making two mistakes. First, your lock and conditions need to be static, or each task will only synchronize and wait on itself. Second, you need to use lock.lock(), not synchronized. It should look like this:
lock.lock();
try {
// wait
// produce/consume
} finally {
lock.unlock();
}

Wait for java async call to complete

I have a async function that calls other async function. In Java, how to wait on untill the async call completes(including any nested async calls in it).
I already Future callable But no luck.
Sample code:
void asyncMehodA(){ }
void asyncMethodB() {
asyncMehodA();
}
I tried the Future callable in the following way:
final Callable<Void> callable1 = new Callable<Void>() {
#Override
public Void call() {
asyncMethodB();
return null;
}
};
final Future<Void> callableFuture = mExecutor.submit(callable1);
try {
callableFuture.get();
} catch (final InterruptedException | ExecutionException e) {}
hoping that the get function will block the execusion untill the async return. But seems the get function will fire the async call and reurn null. not waiting for the asycn to complete its execusion. I added log statements in the verified the same. Please correct me if my understanding is wrong. suggest any other concepts that can aid me.
Here is an example using CountDownLatch.
package chapter13;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class BST {
public static void main(String[] args) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final ExecutorService executorService = Executors.newCachedThreadPool();
Runnable runnableA = () -> {
System.out.println("Runnable A");
latch.countDown();
System.out.println("Runnable A finished");
};
Runnable runnableB = () -> {
System.out.println("Runnable B");
executorService.submit(runnableA);
try {
System.out.println("Runnable B waiting for A to complete");
latch.await();
System.out.println("Runnable B finished");
} catch (InterruptedException e) {
System.out.println("Thread interrupted");
Thread.currentThread().interrupt();
}
};
executorService.submit(runnableB);
Thread.sleep(10);
shutDown(executorService);
}
private static void shutDown(ExecutorService executorService) {
executorService.shutdown();
try {
if (!executorService.awaitTermination(1, TimeUnit.SECONDS)) {
executorService.shutdownNow();
}
} catch (InterruptedException e) {
executorService.shutdownNow();
}
}
}
I use Thread.sleep() method to sleep the main thread, because shuting down the pool immediately after task B was submitted, might cause the pool to stop accepting new tasks before task A is submitted by task B.
One way would be to use a java locking method.
An example:
private AtomicBoolean processed = new AtomicBoolean(true) ;
private String result = null ;
public String doAndWait()
{
synchronized(processed) {
doSomethingAsync() ;
processed.wait();
}
return result ;
}
public void doSomethingAsync()
{
...
result="OK";
synchronized(processed) {
processed.notify();
}
}

Best way to implement TimeoutTask

I'm trying to implement a TimeoutTask which will terminate after a given timeout. Here is what I have:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class TimeoutTask {
private Long timeout;
private TimeUnit unit;
public TimeoutTask(Long timeout, TimeUnit unit) {
this.timeout = timeout;
this.unit = unit;
}
public <T> T execute(Callable<T> callable) {
Objects.requireNonNull(timeout, "Timeout");
Objects.requireNonNull(unit, "Time Unit");
Objects.requireNonNull(callable, "Callable");
ExecutorService service =
Executors.newFixedThreadPool(1);
FutureTask<T> task = new FutureTask<T>(callable);
service.execute(task);
try {
return task.get(timeout, unit);
} catch (InterruptedException | ExecutionException | TimeoutException cause) {
if(cause instanceof TimeoutException) {
System.out.println("\nTimeout occured.");
task.cancel(true);
}
} finally {
System.out.println("Finally called.");
service.shutdownNow();
}
return null;
}
public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
Callable<String> callable = () -> {
System.out.print("Enter something: ");
BufferedReader consoleReader =
new BufferedReader(new InputStreamReader(System.in));
String str = consoleReader.readLine();
return str;
};
TimeoutTask timeoutTask = new TimeoutTask(Long.valueOf(10), TimeUnit.SECONDS);
timeoutTask.execute(callable);
}
}
It is working and I can see the message on timeout if there is no input given. However the process doesn't get terminated. Following is the screenshot from Eclipse's console, see the highlighted portion:
When I am giving any input the process terminated immediately. I am calling the FutureTask#cancel from the catch block also from finally block I am asking to shutdown the service by making a call to ExecutorService#shoutDownNow and I can see it is printing that the finally get called.
How can I stop the process being executed any further in case of timeout?
Is there any better approach to achieve this?
I am using Java 8.
Reference:
java native Process timeout
TimeoutTask using ExecutorService
Update
In the answer of Marko Topolnik said that the cause is of non-interrupted Java IO; so I change the Callable as:
Callable<Long> callable = () -> {
long i = 0;
for(; i <Long.MAX_VALUE;i++){
}
return i;
};
For this case the same thing happened.
Note: The Callable is just a sample.
You call
String str = consoleReader.readLine();
which means you are using the classic, blocking, non-interruptible Java IO. The method keeps blocking even after the timeout and shutdownNow() can't touch it, either, because it also just tries to interrupt the thread.
BTW I have run your code and the output shows "Finally called".
Update
Your updated Callable is just as non-interruptible as the original one. You must either call a method which declares to throw InterruptedException (such as Thread.sleep()) or check Thread.interrupted() yourself in the loop.
The issue you need to solve is that System.in.read() does not response to interrupts. See how to bypass this in Heinz M. Kabutz newsletter

What is callable in Java?

The title pretty much sums it.
I want to know the concept and idea of callable . I have read a question here on difference between callable and runnable. but no one show code and give detail what a callable is. I don't want to know the difference between them. I want to know ,
What is a callable ?
When to use them and how to use them .
When they come in action for
Android.
You can check this example:
In this example Callable task returns the name of thread executing the task after one second. We are using Executor framework to execute 100 tasks in parallel and use Future to get the result of the submitted tasks.
package com.journaldev.threads;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class MyCallable implements Callable<String> {
#Override
public String call() throws Exception {
Thread.sleep(1000);
//return the thread name executing this callable task
return Thread.currentThread().getName();
}
public static void main(String args[]){
//Get ExecutorService from Executors utility class, thread pool size is 10
ExecutorService executor = Executors.newFixedThreadPool(10);
//create a list to hold the Future object associated with Callable
List<Future<String>> list = new ArrayList<Future<String>>();
//Create MyCallable instance
Callable<String> callable = new MyCallable();
for(int i=0; i< 100; i++){
//submit Callable tasks to be executed by thread pool
Future<String> future = executor.submit(callable);
//add Future to the list, we can get return value using Future
list.add(future);
}
for(Future<String> fut : list){
try {
//print the return value of Future, notice the output delay in console
// because Future.get() waits for task to get completed
System.out.println(new Date()+ "::"+fut.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
//shut down the executor service now
executor.shutdown();
}
}
You may also check Using Callable to Return Results From Runnables
Callable is similar to Runnable but it returns a result and may throw an exception.
Use them when you expect your asynchronous tasks to return result.
The returned result of asynchronous computation is represented by a Future.
You can check this simple example implemented using FutureTask (which implements RunnableFuture and Future)
public static void main(String[] args) {
// providing an anonymous callable to FutureTask
RunnableFuture<String> future = new FutureTask<String>(
new Callable<String>() {
#Override
public String call() throws InterruptedException {
System.out.println("sleeping");
Thread.sleep(2000);
System.out.println("returning");
return "hello-world";
}
});
Thread t = new Thread(future);
t.start();
try {
// the get Waits if necessary for the computation to complete
System.out.println(future.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}

Categories