我正在使用向上/向下計數鎖存器將程序限制為 6 個線程,它可以工作,但我真正想要的是始終運行 6 個線程。這樣當一個線程死亡時,另一個線程會啟動。在下面的實現中,只要超時到期,它一次只創建 6 個新線程。關于如何構建我的循環來實現這一點的任何建議?int numThreads = 6;CountUpDownLatch threadCounter = new CountUpDownLatch();for(int t = 1; t <= numThreads; t++) { while(list.hasNext()) { new Thread(new ThreadController("processData",list.next())).start(); threadCounter.countUp(); } try { threadCounter.await(5, TimeUnit.MINUTES); } catch (InterruptedException e) { e.printStackTrace(); }}
有沒有比 Await 更好的方法來使用 CountUpDownLatch?
幕布斯6054654
2021-10-27 10:47:09