ForkJoinPool api 详解

本文详细探讨了ForkJoinPool在Java并发编程中的应用,通过实例分析其核心API,帮助读者理解并掌握如何有效利用ForkJoinPool进行多线程任务处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天时间学习CyclicBarrier api,该类是JUC原子包中的类,通过单元测试代码把所有public api方法跑了一遍,大致了解了底层实现,初学乍练,有很多一知半解的地方,待后续有了深入理解再来补充

package test.java.util.concurrent;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.*;

import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
import org.junit.Test;

/**
 * ForkJoinPool的测试类
 *
 * @date 2020-07-19 14:18:13
 */
public class ForkJoinPoolTest {
        /**
        * 使用默认参数初始化线程池
         * java.util.concurrent.ForkJoinPool@14514713
         * [Running, parallelism = 8, size = 0, active = 0, running = 0, steals = 0, tasks = 0, submissions = 0]
        * @Param
        */
        @Test
        public void testConstruct0()throws Exception{
        ForkJoinPool testObj=new ForkJoinPool();
                System.out.println(testObj);
        }
        /**
        * 通过制定并发数参数初始化线程池
        * @Param
        */
        @Test
        public void testConstruct1()throws Exception{
        ForkJoinPool testObj=new ForkJoinPool(11);
                System.out.println(testObj);
        }
        /**
        * 通过并发数,默认线程创建工厂,异常处理器初始化线程池
        * @Param
        */
        @Test
        public void testConstruct2()throws Exception{
        ForkJoinPool testObj=new ForkJoinPool(33,ForkJoinPool.defaultForkJoinWorkerThreadFactory,new ThreadGroup("forkJoin"),false );
                System.out.println(testObj);
        }
        /**
         * 获取默认static代码块初始化的线程池
         * @Param
         */
        @Test
        public void testCommonPool()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                pool.submit(()-> System.out.println(2222));
        }
        /**
         * 执行制定forkJoinTask
         * @Param
         */
        @Test
        public void testInvoke()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                pool.invoke(ForkJoinTask.adapt(()-> System.out.println(2333)));
        }
        /**
         * 执行ForkJoinTask
         * @Param
         */
        @Test
        public void testExecute1()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                pool.execute(ForkJoinTask.adapt(()-> System.out.println(2333)));
        }
        /**
         * 执行runnable
         * @Param
         */
        @Test
        public void testExecute()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                pool.execute(()-> System.out.println(2333));
        }
        /**
         *提交Task
         * @Param
         */
        @Test
        public void testSubmit()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                pool.submit(ForkJoinTask.adapt(()-> System.out.println(2333)));
        }
        /**
         *提交runnbale
         * @Param
         */
        @Test
        public void testSubmit2()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                pool.submit(()-> System.out.println(2333));
        }
        /**
         *提交callable
         * @Param
         */
        @Test
        public void testSubmit3()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.submit(() -> {
                        System.out.println(2333);
                        return "3333";
                }).get());
        }
        /**
         *提交runnbale,并指定返回值
         * @Param
         */
        @Test
        public void testSubmit4()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.submit(() -> System.out.println(2333), "321").get());
        }
        /**
         * 通过集合批量提交任务,得到future集合,批量处理结果
         * @Param
         */
        @Test
        public void testInvokeAll()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                List<Callable<Integer>> set=new ArrayList<>();
                set.add(()-> {System.out.println(1);
                        return 11;});
                set.add(()-> {System.out.println(2);
                        return 12;});
                set.add(()-> {System.out.println(3);
                        return 13;});
                List<Future<Integer>> futures = pool.invokeAll(set);
                for (int i = 0; i < futures.size(); i++) {
                        System.out.println(futures.get(i).get());
                }
        }
        /**
         * 获取线程工厂
         * @Param
         */
        @Test
        public void testGetFactory()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                pool.getFactory().newThread(pool).run();
        }
        /**
         * 线程未处理异常,线程池执行异常处理器
         * @Param
         */
        @Test
        public void testGetUncaughtExceptionHandler()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.getUncaughtExceptionHandler());
        }
        /**
         * 获取并发线程数
         * @Param
         */
        @Test
        public void testGetParallelism()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.getParallelism());
        }
        /**
         * 获取正常并发数
         * @Param
         */
        @Test
        public void testGetCommonPoolParallelism()throws Exception{
                System.out.println(ForkJoinPool.getCommonPoolParallelism());
        }
        /**
         * 获取线程池大小
         * @Param
         */
        @Test
        public void testGetPoolSize()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.getPoolSize());
        }
        /**
         * 获取线程获取任务的模式,默认LIFO即后进先出,否则是FIFO先进先出
         * @Param
         */
        @Test
        public void testGetAsyncMode()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.getAsyncMode());
        }
        /**
         *获取正在执行的线程数
         * @Param
         */
        @Test
        public void testGetRunningThreadCount()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.getRunningThreadCount());
        }
        /**
         *获取当前激活的线程数
         * @Param
         */
        @Test
        public void testGetActiveThreadCount()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.getActiveThreadCount());
        }
        /**
         *查询线程池是否处于空闲状态
         * @Param
         */
        @Test
        public void testIsQuiescent()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.isQuiescent());
        }
        /**
         *返回线程从另一个线程的工作队列中窃取的任务总数的估计值
         * @Param
         */
        @Test
        public void testGetStealCount()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.getStealCount());
        }
        /**
         *,返回所有工作线程工作队列中的所有任务个数
         * @Param
         */
        @Test
        public void testGetQueuedTaskCount()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.getQueuedTaskCount());
        }
        /**
         *获取提交给线程池但还没开始执行的任务个数。就是所有提交队列中任务之和
         * @Param
         */
        @Test
        public void testGetQueuedSubmissionCount()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.getQueuedSubmissionCount());

        }
        /**
         *若getQueuedSubmissionCount不为0,返回true,表示存在任务还在提交队列没被执行
         * @Param
         */
        @Test
        public void testHasQueuedSubmissions()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.hasQueuedSubmissions());
        }
        /**
         *toString
         * @Param
         */
        @Test
        public void testToString()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.toString());
        }
        /**
         * 尝试停止线程池
         * @Param
         */
        @Test
        public void testShutdown()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                pool.shutdown();
        }
        /**
         *尝试立即停止线程池
         * @Param
         */
        @Test
        public void testShutdownNow()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                pool.shutdownNow();
        }
        /**
         * 查询线程池是否终止
         * @Param
         */
        @Test
        public void testIsTerminated()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.isTerminated());
        }
        /**
         *查询线程池是否正在终止
         * @Param
         */
        @Test
        public void testIsTerminating()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.isTerminating());
        }
        /**
         *查询线程池是否终止
         * @Param
         */
        @Test
        public void testIsShutdown()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                pool.shutdown();
                System.out.println(pool.isShutdown());
        }
        /**
         *线程池一秒钟后等待终止
         * @Param
         */
        @Test
        public void testAwaitTermination()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.awaitTermination(5, TimeUnit.SECONDS));
        }
        /**
         *等待线程池空闲
         * @Param
         */
        @Test
        public void testAwaitQuiescence()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
                System.out.println(pool.awaitQuiescence(5, TimeUnit.SECONDS));
        }
        /**
         *  管理当前线程阻塞的方法,暂时不太理解,做标记,以后完善
         * @Param
         */
        @Test
        public void testManagedBlock()throws Exception{
                ForkJoinPool pool=ForkJoinPool.commonPool();
//                ForkJoinPool.managedBlock();
        }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值