测试代码如下:
public class ThreadsTest implements Runnable{private ExecutorService threadPool=Executors.newFixedThreadPool(10);private CyclicBarrier cb=new CyclicBarrier(10,this);private ConcurrentHashMap<String, Integer> map=new ConcurrentHashMap<String,Integer>();// 只能2个线程同时访问final Semaphore semp = new Semaphore(2);public void count(){for(int i=0;i<10;i++){threadPool.execute(new Runnable(){@Overridepublic void run() {// 获取许可try {semp.acquire();} catch (InterruptedException e) {e.printStackTrace();}for(int j=0; j<6;j++){try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("--------j----threadName-----" + j + "====" + Thread.currentThread().getName());}int score=(int)(Math.random()*35+60);map.put(Thread.currentThread().getName(), score);System.out.println(Thread.currentThread().getName()+"的平均成绩为"+score);try {// 访问完后,释放semp.release();cb.await();} catch (InterruptedException | BrokenBarrierException e) {e.printStackTrace();}}});}}@Overridepublic void run() {int result=0;Set<String> set = map.keySet();for(String s:set){result+=map.get(s);}System.out.println("平均成绩为:"+(result/10)+"分");}public static void main(String[] args) {ThreadsTest t=new ThreadsTest();t.count();}}
执行结果如下:
--------j----threadName-----0====pool-1-thread-1
--------j----threadName-----0====pool-1-thread-2
--------j----threadName-----1====pool-1-thread-2
--------j----threadName-----1====pool-1-thread-1
--------j----threadName-----2====pool-1-thread-2
--------j----threadName-----2====pool-1-thread-1
--------j----threadName-----3====pool-1-thread-2
--------j----threadName-----3====pool-1-thread-1
--------j----threadName-----4====pool-1-thread-2
--------j----threadName-----4====pool-1-thread-1
--------j----threadName-----5====pool-1-thread-2
--------j----threadName-----5====pool-1-thread-1
pool-1-thread-1的平均成绩为80
pool-1-thread-2的平均成绩为80
--------j----threadName-----0====pool-1-thread-3
--------j----threadName-----0====pool-1-thread-4
--------j----threadName-----1====pool-1-thread-3
--------j----threadName-----1====pool-1-thread-4
--------j----threadName-----2====pool-1-thread-4
--------j----threadName-----2====pool-1-thread-3
--------j----threadName-----3====pool-1-thread-3
--------j----threadName-----3====pool-1-thread-4
--------j----threadName-----4====pool-1-thread-4
--------j----threadName-----4====pool-1-thread-3
--------j----threadName-----5====pool-1-thread-3
pool-1-thread-3的平均成绩为74
--------j----threadName-----5====pool-1-thread-4
pool-1-thread-4的平均成绩为86
--------j----threadName-----0====pool-1-thread-5
--------j----threadName-----0====pool-1-thread-6
--------j----threadName-----1====pool-1-thread-5
--------j----threadName-----1====pool-1-thread-6
--------j----threadName-----2====pool-1-thread-5
--------j----threadName-----2====pool-1-thread-6
--------j----threadName-----3====pool-1-thread-6
--------j----threadName-----3====pool-1-thread-5
--------j----threadName-----4====pool-1-thread-5
--------j----threadName-----4====pool-1-thread-6
--------j----threadName-----5====pool-1-thread-5
pool-1-thread-5的平均成绩为72
--------j----threadName-----5====pool-1-thread-6
pool-1-thread-6的平均成绩为61
--------j----threadName-----0====pool-1-thread-7
--------j----threadName-----0====pool-1-thread-8
--------j----threadName-----1====pool-1-thread-7
--------j----threadName-----1====pool-1-thread-8
--------j----threadName-----2====pool-1-thread-8
--------j----threadName-----2====pool-1-thread-7
--------j----threadName-----3====pool-1-thread-8
--------j----threadName-----3====pool-1-thread-7
--------j----threadName-----4====pool-1-thread-8
--------j----threadName-----4====pool-1-thread-7
--------j----threadName-----5====pool-1-thread-8
--------j----threadName-----5====pool-1-thread-7
pool-1-thread-8的平均成绩为89
pool-1-thread-7的平均成绩为61
--------j----threadName-----0====pool-1-thread-9
--------j----threadName-----0====pool-1-thread-10
--------j----threadName-----1====pool-1-thread-10
--------j----threadName-----1====pool-1-thread-9
--------j----threadName-----2====pool-1-thread-9
--------j----threadName-----2====pool-1-thread-10
--------j----threadName-----3====pool-1-thread-9
--------j----threadName-----3====pool-1-thread-10
--------j----threadName-----4====pool-1-thread-10
--------j----threadName-----4====pool-1-thread-9
--------j----threadName-----5====pool-1-thread-10
--------j----threadName-----5====pool-1-thread-9
pool-1-thread-9的平均成绩为93
pool-1-thread-10的平均成绩为90
平均成绩为:78分
如果觉得《Java多线程 信号量和屏障实现控制并发线程数量 主线程等待所有线程执行完毕1》对你有帮助,请点赞、收藏,并留下你的观点哦!