异步编程学习之路(六)-Future和Callable原理及使用,linux内核开发工程师面试

/**

  • Waits if necessary for at most the given time for the computation

  • to complete, and then retrieves its result, if available.

  • @param timeout the maximum time to wait

  • @param unit the time unit of the timeout argument

  • @return the computed result

  • @throws CancellationException if the computation was cancelled

  • @throws ExecutionException if the computation threw an

  • exception

  • @throws InterruptedException if the current thread was interrupted

  • while waiting

  • @throws TimeoutException if the wait timed out

*/

V get(long timeout, TimeUnit unit)

throws InterruptedException, ExecutionException, TimeoutException;

get的重载方法,在给定的超时时间内阻塞主线程等待Future结果返回,如果存在则返回结果,不存在则返回null。

二、Callable源码分析


/**

  • A task that returns a result and may throw an exception.

  • Implementors define a single method with no arguments called

  • {@code call}.

  • The {@code Callable} interface is similar to {@link

  • java.lang.Runnable}, in that both are designed for classes whose

  • instances are potentially executed by another thread. A

  • {@code Runnable}, however, does not return a result and cannot

  • throw a checked exception.

  • The {@link Executors} class contains utility methods to

  • convert from other common forms to {@code Callable} classes.

  • @see Executor

  • @since 1.5

  • @author Doug Lea

  • @param the result type of method {@code call}

*/

@FunctionalInterface

public interface Callable {

/**

  • Computes a result, or throws an exception if unable to do so.

  • @return computed result

  • @throws Exception if unable to compute a result

*/

V call() throws Exception;

}

Callable接口返回的结果可能抛出异常,继承者定义了一个单例的没有参数的方法被称为call 。Callable接口与Runnable接口相似,这两个都是为那些可能被其他线程的执行的实例的类设计的 ,Runnable不会返回结果也不会抛出异常,Callable则可以。

三、Future批量处理数据入库


现在存在这样一个业务场景,我们需要将表A查询出来的省市区id替换为具体名称并分批入库,我们的实现思路如下:

  • 首先我们直接取本地省市区表中的数据,将取出来的数据按照省市区三级做一个处理得出结果M。

  • 在查询出结果M的同时通过主线程查询表A的相关的信息。

  • 然后我们需要使用结果M将表A查询出来的省市区id替换为具体的地区名称。

  • 在替换的过程中我们同时监控它替换后的数量,每50条批量入库一次。

1、ThreadPoolTest

/**

  • @Description:通过Future和Callable实现数据批量处理

  • @Author:zhangzhixiang

  • @CreateDate:2018/12/25

  • @Version:1.0

*/

@RunWith(SpringRunner.class)

@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

public class ThreadPoolTest {

/**

  • 省市区<id, name>

*/

public static Map<Integer, String> provinceMap = Maps.newHashMap();

public static Map<Integer, String> cityMap = Maps.newHashMap();

public static Map<Integer, String> areaMap = Maps.newHashMap();

@Autowired

private ProvinceDAO provinceDAO;

@Autowired

private ClueInfoDAO clueInfoDAO;

@Test

public void test() throws Exception {

//1、定义线程池

ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100), new ThreadPoolExecutor.DiscardPolicy());

//2、异步获取省市区数据

Callable<List> provinceCallable = () -> {

List provinceDoList = provinceDAO.getProvince();

for(ProvinceDO data : provinceDoList) {

if (data.getLevel() == 1) {

provinceMap.put(data.getId(), data.getAreaName());

}

if (data.getLevel() == 2) {

cityMap.put(data.getId(), data.getAreaName());

}

if (data.getLevel() == 3) {

areaMap.put(data.getId(), data.getAreaName());

}

}

return null;

};

//3、获取省份Future值

Future<List> provinceFuture = threadPoolExecutor.submit(provinceCallable);

//4、异步获取线索表数据

Callable<List> clueCallable = () -> clueInfoDAO.selectByCondition(null);

//5、获取线索Future值

Future<List> clueFuture = threadPoolExecutor.submit(clueCallable);

//6、组装并替换省市区

List realClueList = clueFuture.get(15, TimeUnit.SECONDS);

if (provinceFuture.isDone()) {

List arrangementList = Lists.newArrayList();

for (ClueInfoDO clueInfoDO : realClueList) {

Arrangement arrangement = new Arrangement();

arrangement.setClueName(clueInfoDO.getName());

if (clueInfoDO.getProvince() != null) {

arrangement.setProvinceName(provinceMap.get(clueInfoDO.getProvince().getId()));

}

if (clueInfoDO.getCity() != null) {

arrangement.setCityName(cityMap.get(clueInfoDO.getCity().getId()));

}

if (clueInfoDO.getArea() != null) {

arrangement.setAreaName(areaMap.get(clueInfoDO.getArea().getId()));

}

arrangementList.add(arrangement);

}

int runSize = 50;

int handleSize = arrangementList.size() / runSize;

try {

List newList = null;

CountDownLatch countDownLatch = new CountDownLatch(runSize);

for (int i = 0; i < runSize + 1; i++) {

if (i == runSize) {

int startIndex = i * handleSize;

int endIndex = arrangementList.subList(startIndex, endIndex);

newList = arrangementList.subList(startIndex, endIndex);

} else {

int startIndex = i * handleSize;

int endIndex = (i + 1) * handleSize;

newList = arrangementList.subList(startIndex, endIndex);

}

threadPoolExecutor.execute(new ArrangementRunnable(newList, countDownLatch));

}

countDownLatch.await();

threadPoolExecutor.shutdown();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

2、ArrangementRunnable

/**

  • @Description:入库

  • @Author:zhangzhixiang

  • @CreateDate:2018/12/25

  • @Version:1.0

*/

public class ArrangementRunnable implements Runnable {

private List list;

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
img

最后

看完美团、字节、腾讯这三家的面试问题,是不是感觉问的特别多,可能咱们又得开启面试造火箭、工作拧螺丝的模式去准备下一次的面试了。

开篇有提及我可是足足背下了1000道题目,多少还是有点用的呢,我看了下,上面这些问题大部分都能从我背的题里找到的,所以今天给大家分享一下互联网工程师必备的面试1000题

注意不论是我说的互联网面试1000题,还是后面提及的算法与数据结构、设计模式以及更多的Java学习笔记等,皆可分享给各位朋友

最新“美团+字节+腾讯”一二三面问题,挑战一下你能走到哪一面?

互联网工程师必备的面试1000题

而且从上面三家来看,算法与数据结构是必备不可少的呀,因此我建议大家可以去刷刷这本左程云大佬著作的《程序员代码面试指南 IT名企算法与数据结构题目最优解》,里面近200道真实出现过的经典代码面试题

最新“美团+字节+腾讯”一二三面问题,挑战一下你能走到哪一面?

螺丝的模式去准备下一次的面试了。

开篇有提及我可是足足背下了1000道题目,多少还是有点用的呢,我看了下,上面这些问题大部分都能从我背的题里找到的,所以今天给大家分享一下互联网工程师必备的面试1000题

注意不论是我说的互联网面试1000题,还是后面提及的算法与数据结构、设计模式以及更多的Java学习笔记等,皆可分享给各位朋友

[外链图片转存中…(img-siuIHQi2-1711108875742)]

互联网工程师必备的面试1000题

而且从上面三家来看,算法与数据结构是必备不可少的呀,因此我建议大家可以去刷刷这本左程云大佬著作的《程序员代码面试指南 IT名企算法与数据结构题目最优解》,里面近200道真实出现过的经典代码面试题

[外链图片转存中…(img-YDBVdAqL-1711108875742)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 28
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值