"Java Concurrent Programming Combat" chapter sixth task run reading notes

Source: Internet
Author: User


One, running tasks in the thread
Insufficient creation of unlimited threads. The thread life cycle is expensive. Resource consumption. Stability
Second, the executor framework executor based on the producer-consumer model, the operation of submitting tasks is equivalent to the producer. The thread that runs the task is equivalent to the consumer.
1. Executors back to Executorservice
2. Executorservice method Submit, execute
3. Executorservice.submit Return to future
Thread pool, Executors method introduction
Method name Explain
Newfixedthreadpool A fixed-length thread pool is created that creates a thread whenever a task is committed and knows the maximum number of threads to reach the thread pool. The thread pool will no longer change in size (assuming that one of the threads has ended because of an unexpected exception, the thread pool will replenish a new thread.) )
Newcachedthreadpool A cacheable thread pool will be created, assuming that the current size of the thread pool exceeds the processing requirements. Then the spare thread is recycled. When a requirement is added, it is possible to join a new thread, and the thread pool size does not exist regardless of the limit.

Newsinglethreadexecutor A single thread of executor will be created. It creates a single worker thread to run the task, assuming that the thread ends abnormally, and that there is a thread instead. Newsinglethreadexecutor ensures serial operation (e.g. FIFO, LIFO, priority) according to the order of the tasks in the queue
Newscheduledthreadpool Creates a fixed-length thread pool and runs the task in a deferred or timed manner, similar to a timer.



The life cycle of executor
All four of the above methods return the Executorservice,executorservice life cycle in 3 states: Execute, close, and terminated.

Method name Explain
Shutdown will run a smooth shutdown process: No longer accepts new tasks. At the same time, wait for the task that has been submitted to run-contains tasks that have not yet started running.
Shutdownnow The brute-down process will run: It will attempt to cancel all running tasks, and no longer start a task in the queue that has not yet started running.


The Timer class is responsible for managing deferred tasks
Third, find out the available parallelism of 1. The task is split into multiple sub-threading processes. 2. Task callable and Futrue carrying results
Executor 4 Life cycle of running a task: create. Submit. Start. Complete.
means of communication between the submitter and the runner of the task
Executorservice executor = Executors.newsinglethreadexecutor (); callable<object> task = new callable<object> () {public     Object call () throws Exception {          object Resul t = "...";          return result;}     ; future<object> future = Executor.submit (Task); Future.get ();  Wait until the future<object> future = Executor.submit (Task),//wait until the task is finished and return the result//assume that the task is running in error, This will throw Executionexceptionfuture.get ();//wait 3 seconds, after timeout will throw Timeoutexceptionfuture.get (3,  timeunit.seconds); callable<object> task = new callable<object> () {public     Object call () throws Exception {          object Resul t = ...;          return result;}     ;

There are two kinds of tasks:
Runnable
Callable-tasks that require a return value

Task submitter The tasks to executor operation, they need a means of communication, such a means of detailed realization. Usually called the future.

The future usually contains a get (blocked until the task is finished). Cancel Get (timeout) (wait for some time)
Wait a minute. The future is also used for asynchronous synchronization scenarios.

3, 4. Limitations in heterogeneous task parallelism assume that a task is to read an IO resource. Ability to use multiple threads to read at the same time. However, the efficiency limit may be on the IO. It is not possible to exceed the maximum IO read speed even if the total speed of the open and multi-thread reads.

Turning on multiple threads itself also makes programming difficult, and opening multiple threads at the same time can cause resource consumption. Multithreading improves efficiency very often, not adding one thread more efficiently, and possibly increasing the efficiency is negligible.


5. Executor and blockingqueue assume that a set of computational tasks is submitted and that the results are obtained after the calculation is complete, and that the future of each task can be saved using Blockingqueue.


7. Set the time limit for the task//wait 3 seconds, after timeout will throw TimeoutException
Future.get (3, timeunit.seconds);

8. Executorservice.invokeall ()
Runs the given task when all tasks are complete. Returns a future list that maintains the status and results of the task. Returns the Future.isdone () of all elements of the list to true.

Note that the completed task can be terminated either normally or by throwing an exception. Assuming that the given collection has been altered while this operation is in progress, the result of this method is indeterminate.

Iv. information:Win Shaojin-Java Concurrent Programming tutorial Excerpt " task submitter and runner ". " means of communication between the submitter and the runner of the task "
talk to me. Concurrency (iii) analysis and use of--java thread pool (principle)
Http://www.infoq.com/cn/articles/java-threadPool

Java (Android) thread pool (using)
http://www.trinea.cn/android/java-android-thread-pool/

Java Thread Pool Example using executors and Threadpoolexecutor
Http://www.journaldev.com/1069/java-thread-pool-example-using-executors-and-threadpoolexecutor

tool to see the number of threads
Jconsole.exe

"Java Concurrent Programming Combat" chapter sixth task run reading notes

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.