Commit 6456a84a authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

Callable vs Runnable

parent d889e054
import java.util.concurrent.*;
public class SubmitCallable {
public static void main(String arg[]){
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future future =executorService.submit(newRunnable(" Task")); //not blocked
System.out.println(future.isDone());
try {
String msg = (String) future.get(); //blocked
System.out.println(msg);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
System.out.println(future.isDone());
executorService.shutdown();
}
private static Callable newRunnable(String msg){
return new Callable() {
@Override
public Object call() throws Exception {
String report = Thread.currentThread().getName()+" :"+msg;
return report;
}
};
}
}
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class SubmitRunnable {
public static void main(String arg[]){
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future future =executorService.submit(newRunnable(" Task")); //not blocked
System.out.println(future.isDone());
try {
future.get(); //blocked
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
System.out.println(future.isDone());
executorService.shutdown();
}
private static Runnable newRunnable(String msg){
return new Runnable() {
@Override
public void run() {
String report = Thread.currentThread().getName()+" :"+msg;
System.out.println(report);
}
};
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment