java moreexecutor_MoreExecutors工具类使用

MoreExecutors是guava提供的工具类,是对jdk自带的Executors工具类的扩展,主要方法如下:

1、addDelayedShutDown()方法的两个重载:

public static void addDelayedShutDownHook(ExecutorService service, Duration terminationTimeout)

public static void addDelayedShutDownHook(ExecutorService service, long terminationTimeout, TimeUnit unit)

给线程池增加一个关闭钩子,在线程池中的线程是守护线程(daemon thread)时有用,用户线程(user thread)执行完后,jvm不会立即关闭,而是等待一定时间。正常情况下,只要用户线程一结束,jvm就会立即关闭,而不管守护线程任务是否执行完毕。从这里也可以看出,尽量不要把自定义线程搞成守护线程,不然是作死。

示例:

public static voidmain(String[] args) {

ThreadFactory threadFactory= new ThreadFactoryBuilder().setDaemon(true).setNameFormat("async-pool-%d").build();

ExecutorService executorService= new ThreadPoolExecutor(10, 20, 0, TimeUnit.MINUTES, new LinkedBlockingQueue<>(3000), threadFactory);

executorService.submit(()->{try{

Thread.sleep(2000);

System.out.println(Thread.currentThread().getName()+ "@666");

}catch(InterruptedException e) {

e.printStackTrace();

}

});

System.out.println(Thread.currentThread().getName()+ "@888");

MoreExecutors.addDelayedShutdownHook(executorService,3000, TimeUnit.MILLISECONDS);

}

上例中,如果没有调用MoreExecutors.addDelayedShutdownHook()方法的话,只会打印888,不会打印666。因为打印888的线程是用户线程,打印666的线程是守护线程(setDaemon(true)),用户线程一执行完,jvm就关闭了,所以不会有2000ms之后的666打印。假如创建ThreadFactory实例时,没有调用setDaemon(true),即创建的线程是非守护线程,那么会先打印main@888,2000ms后会打印async-pool-0@666,因为main线程和async-pool-0线程都是用户线程。调用MoreExecutors.addDelayedShutdownHook()方法后,jvm会在用户线程结束后等待一段时间再关闭,这段之间守护线程可以工作,到了时间,jvm关闭,守护线程也完蛋了,事情干多少是多少,没干完就没干完。

2、getExitingExecutorService()方法的三个重载:把一个ThreadPoolExecutor实例转成一个应用结束后自动退出的ExecutorService实例。

public static ExecutorService getExitingExecutorService(ThreadPoolExecutor executor)

public static ExecutorService getExitingExecutorService(ThreadPoolExecutor executor, Duration terminationTimeout)

public static ExecutorService getExitingExecutorService(ThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit)

示例:

public static voidmain(String[] args) {

ThreadFactory threadFactory= new ThreadFactoryBuilder().setNameFormat("async-pool-%d").build();

ThreadPoolExecutor threadPoolExecutor= new ThreadPoolExecutor(10, 20, 0, TimeUnit.MINUTES, new LinkedBlockingQueue<>(3000), threadFactory);

ExecutorService executorService=MoreExecutors.getExitingExecutorService(threadPoolExecutor);

executorService.submit(()->{try{

Thread.sleep(110000);

System.out.println(Thread.currentThread().getName()+ "@666");

}catch(InterruptedException e) {

e.printStackTrace();

}

});

System.out.println(Thread.currentThread().getName()+ "@888");

}

在用户线程执行完后,jvm会至多等待一定时间(默认是120s),以期线程池线程执行结束。这里不要求线程池中线程是守护线程,因为不管是不是守护线程,getExitingExecutorService()方法都会把这个线程池包装成守护线程的线程池,并且加个默认120s的关闭钩子。

3、getExitingScheduledExecutorService()方法的三个重载:道理同getExitingExecutorService()方法差不多

public static ScheduledExecutorService getExitingScheduledExecutorService(ScheduledThreadPoolExecutor executor)

public static ScheduledExecutorService getExitingScheduledExecutorService(ScheduledThreadPoolExecutor executor, Duration terminationTimeout)

public static ScheduledExecutorService getExitingScheduledExecutorService(ScheduledThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit)

4、listeningDecorator()方法的两个重载:

public static ListeningExecutorService listeningDecorator(ExecutorService delegate):把一个ExecutorService实例转成一个ListeningExecutorService实例

示例:

public static voidmain(String[] args) {

ThreadFactory threadFactory= new ThreadFactoryBuilder().setNameFormat("async-pool-%d").build();

ThreadPoolExecutor threadPoolExecutor= new ThreadPoolExecutor(10, 20, 0, TimeUnit.MINUTES, new LinkedBlockingQueue<>(3000), threadFactory);

ListeningExecutorService listeningExecutorService=MoreExecutors.listeningDecorator(threadPoolExecutor);

ListenableFuture future= listeningExecutorService.submit(() ->{try{

Thread.sleep(4000);

System.out.println(Thread.currentThread().getName()+ "@666");

}catch(InterruptedException e) {

e.printStackTrace();

}

},1);

Futures.addCallback(future,newFutureCallback() {

@Overridepublic voidonSuccess(Object result) {

System.out.println(Thread.currentThread().getName()+ "@" +result);

}

@Overridepublic voidonFailure(Throwable t) {

System.out.println(Thread.currentThread().getName()+ "@" +t.getMessage());

}

}, threadPoolExecutor);

System.out.println(Thread.currentThread().getName()+ "@888");

}

ListeningExecutorService实例的submit()方法返回值是一个ListenableFuture实例,利用Futures工具类可以给这个实例添加callback回调。

public static ListeningScheduledExecutorService listeningDecorator(ScheduledExecutorService delegate):把一个ScheduledExecutorService实例转成一个ListeningScheduledExecutorService实例

5、directExector()方法:direct是直接的意思。

public static Executor directExecutor():返回一个Executor实例,具体是DirectExecutor类型,DirectExecutor是一个实现了Executor接口的枚举类。调用execute(Runnable command)方法时,在当前线程执行任务,而不会另起一个线程。这个方法没卵用,在当前线程执行的话,直接写代码就好了,干嘛还整这个。

6、newDirectExecutorService()方法

public static ListeningExecutorService newDirectExecutorService():返回一个ListeningExecutorService实例,具体是DirectExecutorService类型,DirectExecutorService是MoreExecutors的内部类,继承了AbstractListeningExecutorService。和DirectExecutor实例类似,调用DirectExecutorService实例的submit()方法时,会在当前线程执行任务,而不会另起一个线程。同样,没卵用。这里还要吐槽一下DirectExecutorService类名的不规范,妈的,明明是ListeningExecutorService实现类,就不能叫DirectListeningExecutorService吗。

7、newSequentialExecutor()方法:

public static Executor newSequentialExecutor(Executor delegate):把一个Executor实例包装成一个顺序执行的Executor实例,具体是SequentialExecutor类型。线程池按照任务添加顺序执行任务。

示例:

public static voidmain(String[] args) {

ThreadFactory threadFactory= new ThreadFactoryBuilder().setNameFormat("async-pool-%d").build();

ThreadPoolExecutor threadPoolExecutor=

new ThreadPoolExecutor(10, 20, 0, TimeUnit.MINUTES, new LinkedBlockingQueue<>(3000), threadFactory);

Executor executor=MoreExecutors.newSequentialExecutor(threadPoolExecutor);for (int i = 0; i < 10; i++) {int d =i;

executor.execute(()->{try{

Thread.sleep(d* 1000);

System.out.println(

Thread.currentThread().getName()+ "@" + d + ", now= " +System.currentTimeMillis());

}catch(InterruptedException e) {

e.printStackTrace();

}

});

}

}

线程池按照任务添加顺序执行任务。上一个任务执行不完,下一个任务就不会开始。活活把一个线程池包装成了一个单线程的线程池,鸡肋。

8、platformThreadFactory()方法:

MoreExecutors.platformThreadFactory():返回默认的ThreadFactory实例。内部调用Executors.defaultThreadFactory(),返回DefaultThreadFactory实例。DefaultThreadFactory是Executors的内部类,实现了ThreadFactory接口。DefaultThreadFactory对于的线程名形如pool-0-thread-0,pool-0-thread-1,我们自定义线程池时线程的名字绝对不可能这么没有意义,所以platformThreadFactory()这个方法也是鸡肋。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值