今天时间学习ScheduledThreadPoolExecutor api,该类是JUC原子包中的类,通过单元测试代码把所有public api方法跑了一遍,大致了解了底层实现,初学乍练,有很多一知半解的地方,待后续有了深入理解再来补充
package test.java.util.concurrent;
import java.util.concurrent.*;
import org.junit.Test;
/**
* ScheduledThreadPoolExecutor的测试类
*
* @date 2020-07-27 22:40:10
*/
public class ScheduledThreadPoolExecutorTest {
/**
* 使用指定参数初始化线程池大小,其他默认
* @Param
*/
@Test
public void testConstruct0()throws Exception{
ScheduledThreadPoolExecutor testObj=new ScheduledThreadPoolExecutor(3);
testObj.submit(()-> System.out.println(111));
}
/**
*使用指定参数初始化线程池大小,使用指定线程工厂,其他默认
* @Param
*/
@Test
public void testConstruct1()throws Exception{
ScheduledThreadPoolExecutor testObj=new ScheduledThreadPoolExecutor(3, Executors.defaultThreadFactory());
testObj.submit(()-> System.out.println(111));
}
/**
* 使用指定参数初始化线程池大小,使用指定拒绝策略
* @Param
*/
@Test
public void testConstruct2()throws Exception{
ScheduledThreadPoolExecutor testObj=new ScheduledThreadPoolExecutor(3,new ThreadPoolExecutor.CallerRunsPolicy());
testObj.submit(()-> System.out.println(111));
}
/**
*使用指定参数初始化线程池大小,使用指定线程工厂,使用指定拒绝策略
* @Param
*/
@Test
public void testConstruct3()throws Exception{
ScheduledThreadPoolExecutor testObj=new ScheduledThreadPoolExecutor(3,Executors.defaultThreadFactory(),new ThreadPoolExecutor.CallerRunsPolicy());
testObj.submit(()-> System.out.println(111));
}
/**
* 延迟多长时间执行
* @Param
*/
@Test
public void testSchedule()throws Exception{
ScheduledThreadPoolExecutor testObj=new ScheduledThreadPoolExecutor(3,Executors.defaultThreadFactory(),new