学习java多线程,请同时参阅 Java多线程 信号量和屏障实现控制并发线程数量,主线程等待所有线程执行完毕1
测试代码如下:
public class ThreadsTest { public static void main(String[] args) throws WriteException, IOException { Listlist = new ArrayList<>(); // 模拟20个客户端访问 for (int i=0;i<10;i++) { Runnable run = new Runnable() { @Override public void run() { try { for(int j=0; j<6;j++){ Thread.sleep(100); System.out.println("--------j----threadName-----" + j + "====" + Thread.currentThread().getName()); } } catch (InterruptedException e) { e.printStackTrace(); } } }; list.add(run); } System.out.println("------------都结束了?????????------------"); for(int j=0;j 实际输出如下:
------------都结束了?????????------------
--------j----threadName-----0====Thread-0
--------j----threadName-----1====Thread-0
--------j----threadName-----2====Thread-0
--------j----threadName-----3====Thread-0
--------j----threadName-----4====Thread-0
--------j----threadName-----5====Thread-0
--------j----threadName-----0====Thread-1
--------j----threadName-----1====Thread-1
--------j----threadName-----2====Thread-1
--------j----threadName-----3====Thread-1
--------j----threadName-----4====Thread-1
--------j----threadName-----5====Thread-1
--------j----threadName-----0====Thread-2
--------j----threadName-----1====Thread-2
--------j----threadName-----2====Thread-2
--------j----threadName-----3====Thread-2
--------j----threadName-----4====Thread-2
--------j----threadName-----5====Thread-2
--------j----threadName-----0====Thread-3
--------j----threadName-----1====Thread-3
--------j----threadName-----2====Thread-3
--------j----threadName-----3====Thread-3
--------j----threadName-----4====Thread-3
--------j----threadName-----5====Thread-3
--------j----threadName-----0====Thread-4
--------j----threadName-----1====Thread-4
--------j----threadName-----2====Thread-4
--------j----threadName-----3====Thread-4
--------j----threadName-----4====Thread-4
--------j----threadName-----5====Thread-4
--------j----threadName-----0====Thread-5
--------j----threadName-----1====Thread-5
--------j----threadName-----2====Thread-5
--------j----threadName-----3====Thread-5
--------j----threadName-----4====Thread-5
--------j----threadName-----5====Thread-5
--------j----threadName-----0====Thread-6
--------j----threadName-----1====Thread-6
--------j----threadName-----2====Thread-6
--------j----threadName-----3====Thread-6
--------j----threadName-----4====Thread-6
--------j----threadName-----5====Thread-6
--------j----threadName-----0====Thread-7
--------j----threadName-----1====Thread-7
--------j----threadName-----2====Thread-7
--------j----threadName-----3====Thread-7
--------j----threadName-----4====Thread-7
--------j----threadName-----5====Thread-7
--------j----threadName-----0====Thread-8
--------j----threadName-----1====Thread-8
--------j----threadName-----2====Thread-8
--------j----threadName-----3====Thread-8
--------j----threadName-----4====Thread-8
--------j----threadName-----5====Thread-8
--------j----threadName-----0====Thread-9
--------j----threadName-----1====Thread-9
--------j----threadName-----2====Thread-9
--------j----threadName-----3====Thread-9
--------j----threadName-----4====Thread-9
--------j----threadName-----5====Thread-9
都结束了