public class ConcurrentTest {
/**
* 多線程測(cè)試用例
* @param threadCount 開啟線程數(shù)
* @param run 線程執(zhí)行方法
* @param finish 線程全部執(zhí)行結(jié)束后運(yùn)行方法
*/
public void begin(int threadCount,Run run,Finish finish){
CountDownLatch countDownLatch = new CountDownLatch(1);
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(threadCount);
for (int i = 0; i < threadCount; i++) {
fixedThreadPool.execute(()->{
try {
countDownLatch.await();
run.run();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
countDownLatch.countDown();
fixedThreadPool.shutdown();
while (!fixedThreadPool.isTerminated()){ }
finish.finish();
}
}
public interface Run {
void run();
}
public interface Finish {
void finish();
}
public class Test {
static int m = 0;
public static void main(String[] args) {
AtomicInteger integer = new AtomicInteger(0);
new ConcurrentTest().begin(10,()->{
for (int i = 0; i < 10000; i++) {
// integer.addAndGet(1);
m++;
}
},()-> System.out.println(m));
}
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。