简体   繁体   中英

Use of Executor Service

Do the threads in executor service run in parallel and are not time sliced when a task is submitted to them? If they don't run in parallel then what's the use of executor service?

An ExecutorService is simply an abstraction over performing asynchronous operations. It takes care of managing incoming tasks and spreading them over the available threads.

As for the threads themselves, it depends on the environment, ie the machine, os and jvm. If you have a single processor with one core, or if the os doesn't support it, you're not going to do stuff in parallel. However on modern machines you'll definitely experinece parallel execution to some degree. Remember that even if you create threads manually, there's no guarantee that they will be executed in parallel by the operating system.

From Java docs:

An Executor that provides methods to manage termination and methods that can produce a Future for tracking progress of one or more asynchronous tasks.

Generally, ALL Callables submitted to an ExecutorService will always be run parallel to the main thread. As for submitted tasks in an ExecutorService , they can be parallel depending on how many threads are specified to run in the service.

If you are using an executor service that only has 1 Thread , then no matter how many tasks you submit, only one will be run at any given time on that executor service .

If you have an executor service that uses 5 Threads , then at most 5 submitted tasks will be running in parallel to each other.

Here's a sample code:

public class ExecutorServiceTest {
public static ExecutorService service = Executors.newFixedThreadPool(5);
public static void main(String[] args) throws Exception {
    System.out.println("Submitting!");

    service.submit(new PrintCallable("Callable 1"));
    service.submit(new PrintCallable("Callable 2"));
    service.submit(new PrintCallable("Callable 3"));
    service.submit(new PrintCallable("Callable 4"));
    service.submit(new PrintCallable("Callable 5"));

    service.shutdown();
    service.awaitTermination(10, TimeUnit.SECONDS);

    System.out.println("Done!");
}

private static class PrintCallable implements Callable<Void> {

    private final String toPrint;

    private PrintCallable(String toPrint) {
        this.toPrint = toPrint;
    }

    @Override
    public Void call() throws Exception {
        for(int i=0; i<5; i++) {
            Thread.sleep(100);
            System.out.println(toPrint);
        }
        return null;
    }

}

}

Running that code will print a "Callable x" in an interweaving manner.

Note: I'm sure this has been asked before, but I can't find anything that closely resembles this question.

Executor service is a sophisticated thread pool and allow execute several independent tasks in background.

If they are processed in parallel or not, depends on how many cpu available units you have in your machine, and the exact use of those cpu is out of programmer control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM