今天时间学习Executors api,该类是JUC原子包中的类,通过单元测试代码把所有public api方法跑了一遍,大致了解了底层实现,初学乍练,有很多一知半解的地方,待后续有了深入理解再来补充
package test.java.util.concurrent;import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;import org.junit.Test;/*** Executors的测试类** @date 2020-07-16 21:37:41*/
public class ExecutorsTest {/*** 固定线程数的线程池,ThreadPoolExecutor和LinkedBlockingQueue初始化* @Param*/@Testpublic void testNewFixedThreadPool1()throws Exception{ExecutorService executor=Executors.newFixedThreadPool(2);executor.submit(()-> {try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}});}/***具有抢占式操作的线程池,ForkJoinPool和parallelism为并发数* @Param*/@Testpublic void testNewWorkStealingPool1()throws Exception{ExecutorService executor=Executors.newWorkStealingPool(3);executor.submit(()-> {try {System.out.println(3333);
// TimeUnit.SECONDS.sleep(1);System.out.println(222);} catch (Exception e) {e.printStackTrace();}});}/***具有抢占式操作的线程池,ForkJoinPool和默认并发数为cpu数量* @Param*/@Testpublic void testNewWorkStealingPool()throws Exception{ExecutorService executor=Executors.newWorkStealingPool();executor.submit(()-> {try {System.out.println(3333);
// TimeUnit.SECONDS.sleep(1);System.out.println(222);} catch (Exception e) {e.printStackTrace();}});}/*** 初始化指定线程数的线程池,并且使用线程工厂类Executors.defaultThreadFactory()或privilegedThreadFactory()* @Param*/@Testpublic void testNewFixedThreadPool()throws Exception{ExecutorService executor=Executors.newFixedThreadPool(3,Executors.defaultThreadFactory());executor.submit(()-> {try {System.out.println(3333);
// TimeUnit.SECONDS.sleep(1);System.out.println(222);} catch (Exception e) {e.printStackTrace();}});}/***使用FinalizableDelegatedExecutorService初始化单一线程数的线程池ThreadPoolExecutor* @Param*/@Testpublic void testNewSingleThreadExecutor1()throws Exception{ExecutorService executor=Executors.newSingleThreadExecutor();executor.submit(()-> {try {System.out.println(3333);
// TimeUnit.SECONDS.sleep(1);System.out.println(222);} catch (Exception e) {e.printStackTrace();}});}/***使用FinalizableDelegatedExecutorService初始化单一线程数的线程池ThreadPoolExecutor* 初始化线程工厂* @Param*/@Testpublic void testNewSingleThreadExecutor()throws Exception{ExecutorService executor=Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());executor.submit(()-> {try {System.out.println(3333);
// TimeUnit.SECONDS.sleep(1);System.out.println(222);} catch (Exception e) {e.printStackTrace();}});}/***创建一个自动增长的线程池* @Param*/@Testpublic void testNewCachedThreadPool1()throws Exception{ExecutorService executor=Executors.newCachedThreadPool();executor.submit(()-> {try {System.out.println(3333);
// TimeUnit.SECONDS.sleep(1);System.out.println(222);} catch (Exception e) {e.printStackTrace();}});}/***创建一个自动增长的线程池,使用指定线程工厂* @Param*/@Testpublic void testNewCachedThreadPool()throws Exception{ExecutorService executor=Executors.newCachedThreadPool(Executors.defaultThreadFactory());executor.submit(()-> {try {System.out.println(3333);
// TimeUnit.SECONDS.sleep(1);System.out.println(222);} catch (Exception e) {e.printStackTrace();}});}/***DelegatedScheduledExecutorService和ScheduledThreadPoolExecutor初始化延时线程池* @Param*/@Testpublic void testNewSingleThreadScheduledExecutor1()throws Exception{ScheduledExecutorService executor=Executors.newSingleThreadScheduledExecutor();executor.schedule(()-> {try {System.out.println(3333);
// TimeUnit.SECONDS.sleep(1);System.out.println(222);} catch (Exception e) {e.printStackTrace();}},3,TimeUnit.SECONDS);}/***DelegatedScheduledExecutorService和ScheduledThreadPoolExecutor初始化延时线程池* 并初始化线程工厂* @Param*/@Testpublic void testNewSingleThreadScheduledExecutor()throws Exception{ScheduledExecutorService executor=Executors.newSingleThreadScheduledExecutor(Executors.defaultThreadFactory());executor.schedule(()-> {try {System.out.println(3333);
// TimeUnit.SECONDS.sleep(1);System.out.println(222);} catch (Exception e) {e.printStackTrace();}},3,TimeUnit.SECONDS);}/*** 初始化延时线程池ScheduledThreadPoolExecutor* @Param*/@Testpublic void testNewScheduledThreadPool1()throws Exception{ScheduledExecutorService executor=Executors.newScheduledThreadPool(3);executor.schedule(()-> {try {System.out.println(3333);
// TimeUnit.SECONDS.sleep(1);System.out.println(222);} catch (Exception e) {e.printStackTrace();}},3,TimeUnit.SECONDS);}/***初始化延时线程池ScheduledThreadPoolExecutor,同时初始化线程工厂* @Param*/@Testpublic void testNewScheduledThreadPool()throws Exception{ScheduledExecutorService executor=Executors.newScheduledThreadPool(3,Executors.defaultThreadFactory());executor.schedule(()-> {try {System.out.println(3333);
// TimeUnit.SECONDS.sleep(1);System.out.println(222);} catch (Exception e) {e.printStackTrace();}},3,TimeUnit.SECONDS);}/*** 返回一个封装了线程池的线程池对象* @Param*/@Testpublic void testUnconfigurableExecutorService()throws Exception{ExecutorService executor=Executors.unconfigurableExecutorService(Executors.newCachedThreadPool());executor.submit(()-> {try {System.out.println(3333);
// TimeUnit.SECONDS.sleep(1);System.out.println(222);} catch (Exception e) {e.printStackTrace();}});}/***返回一个封装了延时线程池的线程池对象* @Param*/@Testpublic void testUnconfigurableScheduledExecutorService()throws Exception{ScheduledExecutorService executor=Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(3));executor.submit(()-> {try {System.out.println(3333);
// TimeUnit.SECONDS.sleep(1);System.out.println(222);} catch (Exception e) {e.printStackTrace();}});}/*** 默认线程工厂* @Param*/@Testpublic void testDefaultThreadFactory()throws Exception{Executors.defaultThreadFactory().newThread(()-> System.out.println(33)).start();}/***具有相同权限线程工厂* @Param*/@Testpublic void testPrivilegedThreadFactory()throws Exception{Executors.privilegedThreadFactory().newThread(()-> System.out.println(33)).start();}/*** 通过callable执行任务,没有返回值* @Param*/@Testpublic void testCallable1()throws Exception{System.out.println(Executors.callable(() -> System.out.println(333)).call());}/***通过callable执行任务,指定返回值* @Param*/@Testpublic void testCallable2()throws Exception{System.out.println(Executors.callable(() -> System.out.println(333),123).call());}/***通过callable执行PrivilegedAction任务,获取返回值* @Param*/@Testpublic void testCallable3()throws Exception{System.out.println(Executors.callable((PrivilegedAction<?>) () -> {System.out.println(333);return 123;}).call());}/***通过callable执行PrivilegedExceptionAction任务,获取返回值* @Param*/@Testpublic void testCallable()throws Exception{System.out.println(Executors.callable((PrivilegedExceptionAction<?>) () -> {System.out.println(333);return 123;}).call());}/***执行带权限的callable,可以通过callable获取返回值* @Param*/@Testpublic void testPrivilegedCallable()throws Exception{System.out.println(Executors.privilegedCallable(() -> {System.out.println(123);return 3;}).call());}/*** 使用当前类加载器执行callable并通过callable获取返回值* @Param*/@Testpublic void testPrivilegedCallableUsingCurrentClassLoader()throws Exception{System.out.println(Executors.privilegedCallableUsingCurrentClassLoader(() -> {System.out.println(123);return 3;}).call());}
}