• Java 串行接口调用优化


    准备面试总结下

     1.CompletableFuture 

    1. static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 20, 1000L, TimeUnit.MICROSECONDS, new ArrayBlockingQueue<>(100));
    2. public static void main(String[] args) throws ExecutionException, InterruptedException {
    3. CompletableFuture task1 = CompletableFuture.supplyAsync(() -> {
    4. try {
    5. System.out.println("task1");
    6. Thread.sleep(1000);
    7. } catch (InterruptedException e) {
    8. throw new RuntimeException(e);
    9. }
    10. return 1000;
    11. }, poolExecutor);
    12. CompletableFuture task2 = CompletableFuture.supplyAsync(() -> {
    13. try {
    14. System.out.println("task2");
    15. Thread.sleep(2000);
    16. } catch (InterruptedException e) {
    17. throw new RuntimeException(e);
    18. }
    19. return 2000;
    20. }, poolExecutor);
    21. CompletableFuture task3 = CompletableFuture.supplyAsync(() -> {
    22. try {
    23. System.out.println("task3");
    24. Thread.sleep(10000);
    25. } catch (InterruptedException e) {
    26. throw new RuntimeException(e);
    27. }
    28. return 5000;
    29. }, poolExecutor);
    30. Integer result1 = task1.get();
    31. System.out.println(result1);
    32. Integer result2 = task2.get();
    33. System.out.println(result2);
    34. Integer result3 = task3.get();
    35. System.out.println(result3);
    36. CompletableFuture voidCompletableFuture = CompletableFuture.allOf(task1, task2, task3);
    37. poolExecutor.shutdown();
    38. System.out.println("执行完毕");
    39. }

    2.CoutDownLatch 

    1. static HashMap map = new HashMap();
    2. public static void main(String[] args) throws InterruptedException, ExecutionException {
    3. ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 20, 1000L, TimeUnit.MICROSECONDS, new ArrayBlockingQueue<>(100));
    4. CountDownLatch countDownLatch = new CountDownLatch(3);
    5. Future task1 = poolExecutor.submit(new Callable() {
    6. @Override
    7. public Integer call() throws Exception {
    8. System.out.println("任务1");
    9. Thread.sleep(1000);
    10. System.out.println("任务1结束");
    11. countDownLatch.countDown();
    12. return 1000;
    13. }
    14. });
    15. Future task2 = poolExecutor.submit(new Callable() {
    16. @Override
    17. public Integer call() throws Exception {
    18. System.out.println("任务2");
    19. Thread.sleep(500);
    20. System.out.println("任务2结束");
    21. countDownLatch.countDown();
    22. return 500;
    23. }
    24. });
    25. Future task3 = poolExecutor.submit(new Callable() {
    26. @Override
    27. public Integer call() throws Exception {
    28. System.out.println("任务3");
    29. Thread.sleep(2000);
    30. System.out.println("任务3结束");
    31. countDownLatch.countDown();
    32. return 2000;
    33. }
    34. });
    35. map.put("task1", task1.get());
    36. map.put("task2", task1.get());
    37. map.put("task3", task1.get());
    38. System.out.println("线程跑完了");
    39. countDownLatch.await();
    40. System.out.println("---------------任务执行结束-------------");
    41. poolExecutor.shutdown();
    42. }

    3.阻塞获取异步调用结果

    1. public static void main(String[] args) throws ExecutionException, InterruptedException {
    2. ExecutorService executorService = Executors.newFixedThreadPool(3);
    3. Future submit = executorService.submit(new Callable() {
    4. @Override
    5. public String call() throws Exception {
    6. Thread.sleep(1000);
    7. return "Its done";
    8. }
    9. });
    10. while (true){
    11. if (submit.isDone()){
    12. System.out.println(submit.get());
    13. break;
    14. }
    15. System.out.println(submit.isDone());
    16. }
    17. }

    4.除了上面两个方法 还有 CyclicBarrier,Semaphore也都可以实现,可以自己尝试下

  • 相关阅读:
    算法练习-LeetCode 剑指 Offer 33. 二叉搜索树的后序遍历序列
    Java开发必须掌握的运维知识 (九)-- Docker容器监控信息可视化仪表:Grafana
    适用于Linux的Windows子系统(系统安装步骤)
    使用 Python 和机器学习掌握爬虫和情感分析
    深入讲解Netty那些事儿之从内核角度看IO模型(上)
    【水滴计划】:合并两个有序数组、寻找两个正序数组的中位数
    信号完整性(SI)电源完整性(PI)学习笔记(三十)电源分配网路(二)
    19. 机器学习——朴素贝叶斯
    win10环境下搭建QT+opencv
    一位同学的Python大作业【分析当当网书籍价格、出版社、电子书版本占比数据】
  • 原文地址:https://blog.csdn.net/NathanniuBee/article/details/133801008