1. List<String> ips = new ArrayList<String>();
    2. Map<String, Boolean> map = new HashMap<String, Boolean>();
    3. // 线程数量
    4. CountDownLatch latch = new CountDownLatch(ips.size());
    5. System.out.println(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
    6. ips.forEach(ip -> {
    7. Thread thread = new Thread() {
    8. public void run() {
    9. map.put(ip, PortConnectUtil.connect(ip));
    10. // 线程计数器
    11. latch.countDown();
    12. }
    13. };
    14. thread.start();
    15. });
    16. // 1. 所有线程执行完毕再进行下面的操作
    17. latch.await();
    18. System.out.println(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
    19. System.out.println(map);