Android learning Week — Four thread pools in Java and encapsulating thread pools themselves

1. The Thread (Thread)

A thread is the smallest unit of computer scheduling that an operating system can do. It is contained within the process and is the actual operating unit within the process.

The above concepts are best understood, but it doesn’t matter if you don’t understand them. For the moment, think of a thread as a task.

2. Multithreading

When we don’t touch threads, our programs are executed in the main thread. When you think about threads, it’s usually when you need to do time-consuming operations, like network requests. Android will report an error if a network request is placed in the main thread, which is theoretically allowed in Java alone, but will cause the main thread to be forced to wait for the task to complete before continuing, potentially affecting performance.

The solution is to create a new Thread to accomplish this task. There are three main ways to create a new Thread: one is to inherit the Thread class, one is to implement the Runnable interface, and another is to implement the Callable interface. Their merits and demerits are beside the point and will not be repeated.

3. The thread pool

Now that we have a solution, why do we need a thread pool when we need a new thread? If a task opens a new thread, it will cost the system a lot. If new tasks are opened frequently but each task only needs a short time, it will undoubtedly increase the burden of the system, such as the server to respond to the user’s login request. Java encapsulation of thread pool provides a solution for this kind of phenomenon, it is mainly done by a waiting queue Settings, such as my thread pool size is 5, 5 threads are running now, however, so this time to submit a new task will be in a waiting queue, when any one thread after the task has been completed.

4. Jdk-5 Executor framework

After Java 5, concurrent programming introduced a bunch of new apis for starting, scheduling, and managing threads. The Executor framework, introduced in Java 5, uses a thread pool mechanism to control the starting, executing, and closing of threads in the java.util.cocurrent package to simplify concurrent programming. Therefore, after Java 5, starting a Thread through Executor is better than using the Thread start method. In addition to being more manageable and more efficient (Thread pooling and cost saving), there is a key point: This helps avoid the this escape problem — if we start a thread in the constructor, because another task may start executing before the constructor ends, which may access the half-initialized object using Executor in the constructor.

The Executor framework includes: Thread pool, Executor, Executors, ExecutorService, CompletionService, Future, Callable, and more.

Here we mainly use Executors to create the thread pool

Note that we can treat a thread in a thread pool as an object. It’s like a tube, and you put the task into that tube.

4.1 newCachedThreadPool Cache thread pool

public static ExecutorService newCachedThreadPool(a)
Copy the code

As the name suggests, this is a thread pool with caching features:

  • Check if there are IDLE threads in the pool. If there are IDLE threads, add new tasks to the pool. If there are no IDLE threads, add a new thread to the pool and execute new tasks.
  • The default idle time is 60 seconds, after which the thread will be destroyed if no new task uses it.

4.2 newFixedThreadPool Core thread pool

public static ExecutorService newFixedThreadPool(int corePoolSize);
Copy the code

In fact, the core thread pool and cache thread pool are actually the same at the bottom of the system, but the parameters are not consistent, the number of threads in the thread pool is fixed, and IDLE is 0.

  • As with the cache thread pool, idle threads are preferred, although a maximum of corePoolSize threads can exist at the same time.
  • If a thread pool is full and a new task is added to the buffer queue

4.3 newScheduledThreadPool Schedules a thread pool

public static ScheduledExecutorService new ScheduledThreadPool(int corePoolSize)
Copy the code
  • Threads in this thread pool can be scheduled (deferred/periodic)

4.4 newSingleThreadExecutor Singleton thread pool

public static ExecutorService newSingleThreadExecutor(a);
Copy the code
  • A singleton thread pool that contains only one thread
  • The same underlying implementation as cache thread pool, but with different parameters, the number of threads is 1, IDLE is 0s

5. To summarize

In general, CachedTheadPool typically creates as many threads as it needs during program execution, then stops creating new threads when it reclaims old ones, so it’s a reasonable Executor’s first choice, only when this approach causes problems (such as when a large number of long-duration connection-oriented threads are required). Consider using FixedThreadPool. — Thinking in Java