Java concurrent programming Practice Study Notes task execution

Source: Internet
Author: User

Java concurrent programming Practice Study Notes task execution
Chapter 6 task execution 6.1 series of tasks executed in a thread-multithreading-limited multithreading 6.2 The executor framework can prevent high-load programs from consuming too much memory java through bounded queues. lang. concurrent provides a thread pool as part of the executor framework. The executor interface provides a web server based on executor in the producer-consumer mode:

public class TaskExecutionWebServer {    private static final int NTHREADS = 100;    private static final Executor exec            = Executors.newFixedThreadPool(NTHREADS);    public static void main(String[] args) throws IOException {        ServerSocket socket = new ServerSocket(80);        while (true) {            final Socket connection = socket.accept();            Runnable task = new Runnable() {                public void run() {                    handleRequest(connection);                }            };            exec.execute(task);        }    }    private static void handleRequest(Socket connection) {        // request-handling logic here    }}

 

By inheriting the executor interface, you can implement thread pools for different server processing methods: it is closely related to the task queue and stores all tasks to be executed in the work queue. The work thread is established in advance, then, the task execution is retrieved from the work queue. After the execution is complete, wait for the next task again. The work thread can be reused. Call the static method in executors to create a thread pool: newFixedThreadPool newCachedThreadPool newSingleThreadPool newScheduledThreadPool executor life cycle: executorServer, A subinterface of executor, is used to implement lifecycle management of delayed tasks and cyclic tasks: timer or use scheduledThreadPoolExecutor to replace 6.3 to find the available parallel tasks callable and futurecallable that carry results: it thinks that the main entry point (call) will return a value or throw an exception future: indicates the lifecycle of a task, and provides methods to determine whether the task has been completed or whether all the submit methods in the canceled executorServer submit a runnable or callable to the executor, returns a future to obtain the execution of the task. Result or cancel the task. You can also explicitly instantiate a futureTaskcompletionServer for runnable or callable: Combine executor and blockQueue to set the time limit for the task invokeall

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.