import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* 跟着阿里p7学并发,微信公众号:javacode2018
*/
@Slf4j
public class Demo2 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService delegate = Executors.newFixedThreadPool(5);
try {
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(delegate); Liste 需要zi料+ 绿色徽【vip1024b】
nableFuture submit = executorService.submit(() -> { log.info(“{}”, System.currentTimeMillis());
TimeUnit.SECONDS.sleep(4);
//int i = 10 / 0;
log.info(“{}”, System.currentTimeMillis());
return 10;
});
Futures.addCallback(submit, new FutureCallback() {
@Override
public void onSuccess(@Nullable Integer result) {
log.info(“执行成功:{}”, result);
}
@Override
public void onFailure(Throwable t) {
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.error(“执行任务发生异常:” + t.getMessage(), t);
}
}, MoreExecutors.directExecutor());
log.info(“{}”, submit.get());
} finally {
delegate.shutdown();
}
}
}
输出:
14:26:07.938 [pool-1-thread-1] INFO com.itsoku.chat34.Demo2 - 1567491967936
14:26:11.944 [pool-1-thread-1] INFO com.itsoku.chat34.Demo2 - 1567491971944
14:26:11.945 [main] INFO com.itsoku.chat34.Demo2 - 10
14:26:11.945 [pool-1-thread-1] INFO com.itsoku.chat34.Demo2 - 执行成功:10
上面通过调用Futures的静态方法addCallback在异步执行的任务中添加回调,回调的对象是一个FutureCallback,此对象有2个方法,任务执行成功调用onSuccess,执行失败调用onFailure。
失败的情况可以将代码中int i = 10 / 0;注释去掉,执行一下可以看看效果。
示例2:获取一批异步任务的执行结果
package com.itsoku.chat34;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;
/**
* 跟着阿里p7学并发,微信公众号:javacode2018
*/
@Slf4j
public class Demo3 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
log.info(“star”);
ExecutorService delegate = Executors.newFixedThreadPool(5);
try {
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(delegate); List<ListenableFuture> futureList = new ArrayList<>();
for (int i = 5; i >= 0; i–) {
int j = i;
futureList.add(executorService.submit(() -> { TimeUnit.SECONDS.sleep(j); return j;
})); } //获取一批任务的执行结果
List resultList = Futures.allAsList(futureList).get();
//输出
resultList.forEach(item -> {
log.info(“{}”, item);
});
} finally {
delegate.shutdown();
}
}
}
输出:
14:26:35.970 [main] INFO com.itsoku.chat34.Demo3 - star
14:26:41.137 [main] INFO com.itsoku.chat34.Demo3 - 5
14:26:41.138 [main] INFO com.itsoku.chat34.Demo3 - 4
14:26:41.138 [main] INFO com.itsoku.chat34.Demo3 - 3
14:26:41.138 [main] INFO com.itsoku.chat34.Demo3 - 2
14:26:41.138 [main] INFO com.itsoku.chat34.Demo3 - 1
14:26:41.138 [main] INFO com.itsoku.chat34.Demo3 - 0
结果中按顺序输出了6个异步任务的结果,此处用到了Futures.allAsList方法,看一下此方法的声明:
public static ListenableFuture<List> allAsList(
Iterable<? extends ListenableFuture<? extends V>> futures)
传递一批ListenableFuture,返回一个ListenableFuture<List>,内部将一批结果转换为了一个ListenableFuture对象。
示例3:一批任务异步执行完毕之后回调
异步执行一批任务,最后计算其和
package com.itsoku.chat34;
import com.google.common.util.concurrent.*;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList;
总结
在这里,由于面试中MySQL问的比较多,因此也就在此以MySQL为例为大家总结分享。但是你要学习的往往不止这一点,还有一些主流框架的使用,Spring源码的学习,Mybatis源码的学习等等都是需要掌握的,我也把这些知识点都整理起来了
ual.Nullable;
import java.util.ArrayList;
总结
在这里,由于面试中MySQL问的比较多,因此也就在此以MySQL为例为大家总结分享。但是你要学习的往往不止这一点,还有一些主流框架的使用,Spring源码的学习,Mybatis源码的学习等等都是需要掌握的,我也把这些知识点都整理起来了
[外链图片转存中…(img-1dEt5rac-1710358886342)]
[外链图片转存中…(img-fRutbcl0-1710358886343)]