Creating a multithread manager instance in Android

If you want to execute a task repeatedly, with different data sets (different parameters), but only one execution at a time (the task is single threaded), intentservice meets your needs. When you need to automatically execute tasks when resources are available, or allow multiple tasks to be executed at the same time, you need a thread manager to manage your threads. ThreadPoolExecutor maintains a queue. When its thread pool is free, it takes tasks from the queue and executes them. To run a task, all you have to do is add it to the queue.

Thread pools can run multiple instances of a task in parallel, so you need to save code thread safe. Variables that can be accessed by multiple threads need synchronization blocks. For more information, see processes and threads( http://developer.android.com/guide/components/processes-and-threads.html )

Define thread pool classes

In its own class, instance ThreadPoolExecutor. In the class, operate as follows: use the static variable for the thread pool. You may only need a singleton thread pool in the app in order to uniformly control and limit CPU or network resources. If you have different runnable types, you may want each type to have its own thread pool, but these can be put into a single instance. For example, you can declare it as a global variable:

Using the private construction method

Declaring the constructor private ensures singleton access, which means you don't need to encapsulate class access in a synchronized code block.

Call the method in the thread pool class to start the task

A method is defined in the thread pool class to add tasks to the thread pool queue, such as:

Instantiate the handler of a UI thread

Handler is used to communicate with UI threads. Most UI controls are only allowed to be modified in UI threads.

Determine thread pool parameters

Once you have all the class structures, you can start defining thread pools. To instantiate a thread pool object, you need the following values: initial pool size and maximum pool size. The number of threads in the thread pool mainly depends on the number of CPU cores of the device. It can be obtained from the system environment.

This number may not reflect the number of physical CPU cores of the device; Some device CPUs automatically disable some cores according to the system load. For these devices, availableprocessors () returns the number of currently active cores.

Keep active time and time unit the time a process remains idle before shutting down (processes can be reused). The time unit is in timeunit

Task queue

The queue of ThreadPoolExecutor holds the runnable object. When executing code in a thread, the thread pool manager will take a runnable object from a FIFO queue and attach it to the thread. The queue implements the BlockingQueue interface, which is provided when creating a thread pool. You can choose one of the existing implementations to suit your needs. See ThreadPoolExecutor. The following is an example of using linkedblockingqueue:

Create thread pool

Call the threadpoolexecutor() method to initialize the thread pool. It creates a management thread. Because the initial size of the thread pool is the same as the maximum pool size, ThreadPoolExecutor creates all thread objects during initialization, such as:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>