Java Concurrency and Multithreading (ii) Executor framework

Source: Internet
Author: User

Executor frame

1.Task? Thread?

Many people in the learning of multithreading this part of the knowledge, it is easy to confuse the two concepts: tasks (Task) and Threads (thread).

Concurrent programming allows our programs to be divided into separate, separate, and running tasks. These tasks are specifically driven by threads. In Java, the thread class itself does nothing, it simply drives the task assigned to it, and the task is provided by the Runnable interface.

2.executor

Executor is a simple interface, but it provides a standard way to decouple the commit process of a task from the execution of a task, so that execution and modification of the executing policy can be performed and modified for some type of task without too much difficulty. Executor based on the producer and consumer patterns, the task submission is equivalent to the producer, and the thread performing the task is equivalent to the consumer.

3. Thread pool

The thread pool is literally a pool of resources that manages a set of homogeneous worker threads (worker thread). Since threads are responsible for driving tasks, the thread pool is used to handle a bunch of tasks, which are usually stored in a place called the working queue. The task for a worker thread is simple: get a task from the work queue, execute the task, and then return to the thread pool and wait for the next task.

The static factory method of the Java.util.concurrent.Executors class can be used in Java to create a thread pool.

The executor interface provides a separate standard for task submission and task execution. You can therefore use executors to create different thread pools to implement different execution strategies.

4.ExecutorService

The executorservice extends the executor interface and adds some methods for life cycle management.

5.Runnable and Callable Interface

The executor framework uses runnable as its basic task representation. However, Runnable does not return a value or throws a checked exception. Instead, callable can return a value and may throw an exception.

6.Future and Futuretask

The future represents the result of an asynchronous calculation. It provides a way to check whether the calculation is complete, to wait for the calculation to complete, and to get the results of the calculation. You can only use the Get method to get the results after the calculation is complete and, if necessary, block this method before the calculation is complete. Cancellation is performed by the Cancel method. Other methods are provided to determine whether the task is completed properly or canceled.

The Futuretask class is an implementation of the future, and the class also implements the Runnable interface.

7.CompletionService interface

Functionally speaking, Completionservice combines the functions of executor and blockingqueue. A service that separates the production of new asynchronous tasks from the results of using the completed tasks. The task that the producer submit to perform. The consumer take the completed tasks and process their results in the order in which they were completed.

If you do not use Completionservice, you should poll each time you future.get to determine whether the task submitted to executor is complete. It would be nice to take a direct take with Completionservice.

The Executorcompletionservice class implements the interface.

Java Concurrency and Multithreading (ii) Executor framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.