Thread pool tool class encapsulation

Why to use thread pool
1. Frequent creation and destruction of threads consumes resources and time. In other words, thread pools can save resources and time.
2. Some threads take less time to execute tasks than to create and destroy threads.
2. Function of thread pool:
Thread pool is a technology of creating threads in advance. Before the task arrives, the thread pool creates a certain number of threads, joins the idle queue, and reuses these resources to reduce the frequent creation and destruction of objects.

/**
 * Created by zhanggonglin on 2018/11/1.
 */

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Tool class for thread pool management
 */
public class ThreadManager {
    //Manage the thread pool through the agent class of ThreadPoolExecutor
    private static ThreadPollProxy mThreadPollProxy; //Single column object

    public static ThreadPollProxy getThreadPollProxy() {
        synchronized (ThreadPollProxy.class) {
            if (mThreadPollProxy == null) {
                mThreadPollProxy = new ThreadPollProxy(3, 6, 1000);
            }
        }
        return mThreadPollProxy;
    }

    //Manage the thread pool through the agent class of ThreadPoolExecutor
    public static class ThreadPollProxy {
        private ThreadPoolExecutor poolExecutor;//Thread pool executor. java implements thread pool management through this api
        private int corePoolSize;
        private int maximumPoolSize;
        private long keepAliveTime;

        public ThreadPollProxy(int corePoolSize, int maximumPoolSize, long keepAliveTime) {
            this.corePoolSize = corePoolSize;
            this.maximumPoolSize = maximumPoolSize;
            this.keepAliveTime = keepAliveTime;
        } //Provide a way to perform tasks externally

        public void execute(Runnable r) {
            if (poolExecutor == null || poolExecutor.isShutdown()) {
                poolExecutor = new ThreadPoolExecutor(
                        //Number of core threads
                        corePoolSize,
                        //Maximum number of threads
                        maximumPoolSize,
                        //Time to stay active when the thread is idle
                        keepAliveTime, //Time unit,
                        // millisecond
                        TimeUnit.MILLISECONDS,
                        //Thread task queue
                        new LinkedBlockingQueue<Runnable>(),
                        //Create factory for thread
                        Executors.defaultThreadFactory());
            }
            poolExecutor.execute(r);
        }


    }

Usage:

ThreadManager.getThreadPollProxy().execute(new Runnable() {
                    @Override
                    public void run() {
                        //Request network related and other time-consuming operations
                    }
                });

Keywords: Mobile Java less network

Added by jkohns on Thu, 12 Dec 2019 21:17:57 +0200