Skip to content

Instantly share code, notes, and snippets.

@digvijaybhakuni
Created April 17, 2015 13:32
Show Gist options
  • Save digvijaybhakuni/422fe62f6fd643049770 to your computer and use it in GitHub Desktop.
Save digvijaybhakuni/422fe62f6fd643049770 to your computer and use it in GitHub Desktop.
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 MyTextExcutor{
private static class MyCallableClass implements Callable<String> {
public String call() throws Exception{
System.out.println("Callable is Called");
throw new Exception("My Own error");
//return "String";
}
}
public static void main(String[] args) {
// Step1 : Create a Runnable
Callable callableTask = new MyCallableClass();
// Step 2: Configure Executor
// Uses FixedThreadPool executor
ExecutorService executor = Executors.newFixedThreadPool(2);
//Future<String> future =
Future<String> future = executor.submit(callableTask);
Future future1 = executor.submit(new Runnable(){
public void run(){
System.out.println("Runnable is Called");
int i = 5/0;
}
});
try {
while(!future1.isDone()){
System.out.println("running");
}
future1.get();
System.out.println("result");
for (int i = 0; i < 5; i++ ) {
Thread.sleep(100);
System.out.println("Digggu");
}
String result = future.get();
executor.shutdown();
}catch ( InterruptedException e) {
e.printStackTrace();
executor.shutdown();
}catch ( ExecutionException e) {
e.printStackTrace();
System.out.println("ExecutionException");
executor.shutdown();
}
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment