I am attempting to run multiple services parallely using ExecutorService. But i failed to execute parallely.
I have written java.util.concurrent.TimeUnit.MINUTES.sleep(1) to wait one minute in Service1 class.
But Service2 is processing only after Service1 processed.
Below is my code snippet, Kindly correct me/code if my understand about ExecutorService is wrong
public void startService() {
try {
ExecutorService service = Executors.newFixedThreadPool(3);
service.submit(new Service1());
service.submit(new Service2());
service.submit(new Service3());
service.shutdown();
service.awaitTermination(1, TimeUnit.MINUTES);
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public class Service1 implements Callable<Object> {
{
try {
java.util.concurrent.TimeUnit.MINUTES.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public Object call() throws Exception {
return null;
}
}
public class Service2 implements Callable<Object> {
@Override
public Object call() throws Exception {
System.out.println(" Service 2 "); // It prints after 1 minute only.
return null;
}
}
public class Service3 implements Callable<Object> {
@Override
public Object call() throws Exception {
System.out.println(" Service 3 ");
return null;
}
}