6.1 Performing tasks in threads
The first step is to find clear task boundaries. Most server applications provide a natural way to select a task boundary: with separate requests as boundaries.
-6.6.1 Performing tasks serially
The simplest task scheduling strategy is to perform the tasks serially in a single thread.
Although straightforward, only one request can be processed at a time. When the server is processing the request, the incoming connection must wait until the request processing is complete, and the server will call accept again.
-6.1.2 creating a thread for a task in the display
-6.1.3 lack of unlimited creation threads
(1) The thread life cycle is very expensive
(2) Resource consumption
(3) Stability: There is a limit on the number of threads that can be created, which varies depending on the platform and is subject to several factors, including the JVM startup parameters, the size of the requested stack in the thread constructor, and the limitations of the underlying operating system on the thread.
6.2 Executor Frame
Java.util.concurrent provides a flexible thread pool as part of the executor framework.
Executor based on the producer-consumer model, the actions that are submitted to the task are equivalent to the producer, and the thread performing the task equals the consumer.
-6.2.1 Example: Executor-based Web server
By using executor, the submission of a request task is decoupled from the actual execution of the task, and the server's behavior can be easily changed by simply using a different executor implementation:
-6.6.2 Execution Policy
-6.2.3 thread Pool
Create a thread pool by calling the static factory method in executor:
-6.2.4 Executor life cycle
Although I know how to create a executor, I don't know how to close it now. The JVM exits only after all non-daemon threads have been terminated. Therefore, if executor is not properly closed, the JVM will not be able to end. The executor extends the Executorservice interface and adds some methods for life cycle management:
-6.2.5 delay tasks and cycle tasks
The Timer class is responsible for managing deferred tasks and periodic tasks, but there are some flaws, so consider using Scheduledthreadpoolexecutor instead.
PS: Here Spit Trough This book of the Chinese version, 102 pages of the picture in the wrong order, the quality of the Chinese version is not a compliment.
6.3 Finding the parallelism to use
If you use executor, you must represent the task as a runnable. Sometimes the task boundary is not obvious. There may still be an exploration of parallelism in a single user request.
-6.3.1 Example: Serial page renderer
The text element is drawn first, and the rectangle placeholder space is reserved for the image, and after the first text is processed, the program begins to download the image and draws it into the appropriate placeholder space:
We can split the problem into multiple independent tasks and execute concurrently, resulting in higher CPU utilization and response sensitivity.
-6.3.2 tasks for carrying results callable and future
All methods in Executorservice will return a future, submitting a runnable or callable to executor and getting a future to get the result of the task or cancel the task.
-6.3.3 Example: Implementing a page renderer with the future
In order for the page renderer to achieve higher concurrency, the rendering task is first decomposed into two tasks: one is to render all the text, and the other is to download all the images:
-6.3.4 limitations in the parallelism of heterogeneous tasks
-6.3.5 Completionservice:executor and Blockingqueue
Completionservice combines the functions of executor and blockingqueue, and can submit callable tasks to it for execution, and then uses a take and poll method similar to the queue operation to get the results done. These results will be encapsulated into the future as they are completed. Executorcompletionservice implements the Completionservice and delegates the calculated part to a executor. Completionservice and Executorcompletionservice detailed
-6.3.6 Example: Implementing a page renderer using Completionservice
Create a separate task for each download of the image and execute them in the thread pool. In addition, by getting the results from the completionservice and making each picture appear immediately after the download is complete, the user gets a more dynamic and responsive user interface:
-6.3.7 set a time limit for a task
-6.3.8 Example: Travel booking Portal
Summary:
"Java Concurrency Programming Practical" Reading Notes 5--task execution, executor framework