11th chapter Android Threads and thread pool

Source: Internet
Author: User

Common Threading PatternsAsynctask: not suitable for particularly time-consuming background tasks, time consuming thread pooling.                      Must be loaded in the main thread. Android3.0 is serially executed after 3.0, which is parallel in the past. Handerthread: Inherits thread, is a thread that can use handler, and the difference between normal thread is that the message queue can be created through Looper.prepare in the Run method.and Looper.loop () to turn on the message loop.
Intentservice: abstract class, suitable for performing high-priority background tasks. Each background task must be started once intentservice, internally through the way the message to Handerthread request to perform tasks, handler in the Looper is the order of processing messages, so Intentservi Ce is also the sequential execution of background tasks, and when multiple background tasks are present, background tasks are queued for execution in the order in which they originated.
thread pool ThreadpoolexecutorAdvantages: 1. The thread in the reuse thread avoids the performance cost of thread creation and destruction, 2, can effectively control the maximum concurrency of the thread pool, avoid a large number of threads due to each other to seize resources caused by congestion, 3, to manage the thread, provide timed execution and specify interval loop execution and other functions.
  
  1. public ThreadPoolExecutor(int corePoolSize,
  2. int maximumPoolSize,
  3. long keepAliveTime,
  4. TimeUnit unit,
  5. BlockingQueue<Runnable> workQueue,
  6. ThreadFactory threadFactory)
Corepoolsize: Number of core threads
Maximumpoolsize: The thread pool accommodates the maximum number of threads KeepAliveTime: Non-core threads are idle over time, and more than non-core threads are recycled. Unit:keepalivetime parameter Time unit workqueue: thread pool in the task queue, the Runnable object submitted by the thread pooling execute method is stored in this parameter threadfactory: provides the ability to create new threads for the thread pool;
If the number of threads has reached the maximum specified by the thread pool, the task is denied and the caller is called by the rejectedexecution of the Rejectedexecutionhandler.
Category: 1, fixedthreadpool thread pools with fixed threads, which are not recycled when the thread is idle, can quickly respond to an external request. Only the core process. 2, Cachedthreadpool the number of threads is not the core process of the thread pool, thread idle has a timeout mechanism, more than will be recycled. Suitable for performing a large amount of time-consuming tasks. 3, Scheduledthreadpool core thread number fixed, non-core thread number is not limited, when non-core process is idle, it will be recycled. Used to perform scheduled tasks and recurring tasks that have a fixed period. 4. Singlethreadexecutor There is only one core process that ensures that all tasks are executed sequentially in the same thread (FIFO execution);




From for notes (Wiz)

11th chapter Android Threads and thread pool

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.