• 多个线程启动 ,等待全部执行完毕再搜集数据


    前几天在公司的项目上有个同事使用了多线程统计数据,当时出现了一个用户一直使用服务器首次登录信息作为查询信息。找了半天才发现,线程池资源同步了。后面手动将数据set进去的。

    等待线程全部执行完毕,这里使用的是减法计数器,也可而已用加法计数器:CyclicBarrier

    1. package com.quxiao.controller;
    2. import java.util.*;
    3. import java.util.concurrent.*;
    4. import java.util.stream.Collectors;
    5. /**
    6. * @program: package1
    7. * @author: quxiao
    8. * @create: 2023-09-27 15:22
    9. **/
    10. public class t3 {
    11. static final ExecutorService service = Executors.newFixedThreadPool(10);
    12. public static void main(String[] args) throws ExecutionException, InterruptedException {
    13. CountDownLatch countDownLatch = new CountDownLatch(4);
    14. FutureTask task1 = new FutureTask<>(new t(2, countDownLatch));
    15. FutureTask task2 = new FutureTask<>(new t(3, countDownLatch));
    16. FutureTask task3 = new FutureTask<>(new t(4, countDownLatch));
    17. FutureTask task4 = new FutureTask<>(new t(5, countDownLatch));
    18. service.execute(task1);
    19. service.execute(task2);
    20. service.execute(task3);
    21. service.execute(task4);
    22. countDownLatch.await();
    23. System.out.println(task1.get());
    24. System.out.println(task2.get());
    25. System.out.println(task3.get());
    26. System.out.println(task4.get());
    27. // service.shutdown();
    28. }
    29. static class t implements Callable> {
    30. List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
    31. int sum = 0;
    32. CountDownLatch countDownLatch;
    33. public t(int sum, CountDownLatch countDownLatch) {
    34. this.sum = sum;
    35. this.countDownLatch = countDownLatch;
    36. }
    37. @Override
    38. public List call() throws Exception {
    39. TimeUnit.SECONDS.sleep(1L);
    40. List returnList = list.stream().filter(x -> x >= sum).collect(Collectors.toList());
    41. countDownLatch.countDown();
    42. return returnList;
    43. }
    44. }
    45. }

    限流操作,限制人数访问,超过预定值就等待:

    1. package com.quxiao.controller;
    2. import java.util.*;
    3. import java.util.concurrent.*;
    4. import java.util.stream.Collectors;
    5. /**
    6. * @program: package1
    7. * @author: quxiao
    8. * @create: 2023-09-27 15:22
    9. **/
    10. public class t3 {
    11. static final ExecutorService service = Executors.newFixedThreadPool(10);
    12. public static void main(String[] args) throws ExecutionException, InterruptedException {
    13. Semaphore semaphore = new Semaphore(3);
    14. for (int i = 0; i < 9; i++) {
    15. final int t = i;
    16. new Thread(() -> {
    17. try {
    18. semaphore.acquire();
    19. TimeUnit.SECONDS.sleep(5L);
    20. System.out.println(Thread.currentThread().getName() + "进来了");
    21. System.out.println(Thread.currentThread().getName() + "出去了");
    22. System.out.println();
    23. } catch (InterruptedException e) {
    24. throw new RuntimeException(e);
    25. } finally {
    26. semaphore.release();
    27. }
    28. }, "" + i).start();
    29. }
    30. }
    31. }
  • 相关阅读:
    MySQL事务与MVCC如何实现的隔离级别
    大模型之十二十-中英双语开源大语言模型选型
    为了讲明白继承和super、this关键字,群主发了20块钱群红包
    体育馆为何要安装弧光保护装置?其作用何在?
    QT学习_09_事件
    C++文件服务器项目—Nginx+FastDFS插件—5
    盘点52个Python各行各业管理系统源码Python爱好者不容错过
    OMS1664_ OMS1654_ OMS1644_ OMS1634_马可尼 2.5G-10Gb/s
    免费:CAD批量转PDF工具,附下载地址
    Java8实战-总结24
  • 原文地址:https://blog.csdn.net/qx020814/article/details/133466227