fork / join and the number of threads open

// to execute four threads simultaneously
ForkJoinPool myPool = new ForkJoinPool(4);
myPool.submit(() -> {})

The number of threads open

public class ThreadPoolHelper {

    public static ThreadPool instance;
    private ThreadPoolExecutor longExecutor; // takes a long thread pool to request to the network
    private ThreadPoolExecutor shortExecutor; // relatively short thread pool used to load local data

    // get the thread pool object singleton
    public static ThreadPool getInstance() {
        if (instance == null) {
            synchronized (ThreadPoolHelper.class) {
                if (instance == null) {
                    int cpuNum = Runtime.getRuntime () availableProcessors ();. // Get the number of processors
                    int threadNum = cpuNum * 2 + 1; // the number of cpu, calculate a reasonable number of concurrent threads
                    instance = new ThreadPool (threadNum-1, threadNum, Integer.MAX_VALUE); // default is a dual-core cpu one thread per core go a waiting thread
                }
            }
        }
        return instance;
    }

    public static class ThreadPool {
        private ThreadPoolExecutor mExecutor;
        private int corePoolSize; // core number of threads
        private int maximumPoolSize; // maximum number of threads
        private long keepAliveTime; // idle thread survival time

        private ThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime) {
            this.corePoolSize = corePoolSize;
            this.maximumPoolSize = maximumPoolSize;
            this.keepAliveTime = keepAliveTime;
        }

        public void execute(Runnable runnable) {
            if (runnable == null) {
                return;
            }
            if (mExecutor == null) {
                mExecutor = new ThreadPoolExecutor (corePoolSize, // core number of threads
                        maximumPoolSize, // maximum number of threads
                        keepAliveTime, // idle thread survival time
                        TimeUnit.MILLISECONDS, // time unit
                        new LinkedBlockingDeque <Runnable> (Integer.MAX_VALUE), // the thread queue
                        Executors.defaultThreadFactory (), // a thread factory
                        new ThreadPoolExecutor.AbortPolicy () {// queue is full, but more than the current number of threads exception handling strategy when the maximum number of threads
                            @Override
                            public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
                                super.rejectedExecution(r, e);
                            }
                        }
                );
            }
            mExecutor.execute(runnable);
        }


        // remove an object from the thread queue
        public void cancel(Runnable runnable) {
            if (mExecutor != null) {
                mExecutor.getQueue().remove(runnable);
            }
        }
    }


    /**
     * Test use
     */
    @Test
    public void test(){

        ThreadPoolHelper.getInstance().execute(new Runnable(){
            public void run() {
                // write processing business code here

            }

        });
    }

Guess you like

Origin blog.csdn.net/weixin_41825468/article/details/86538595