Item 81. Concurrent Utilities
λκΈ°ν μ₯μΉλ₯Ό μ¬μ©ν μμ
import java.util.concurrent.CountDownLatch;
public class Test {
// λμλ€μ λμμ μμν΄ λͺ¨λ μλ£νλ λ° κ±Έλ¦¬λ μκ°μ μΈ‘μ νλ λ©μλ
public static long time(Executor executor, int concurrency, Runnable action)
throws InterruptedException {
CountDownLatch ready = new CountDownLatch(concurrency);
CountDownLatch start = new CountDownLatch(1);
CountDownLatch done = new CountDownLatch(concurrency);
for (int i = 0; i < concurrency; i++) {
// concurrencyλ§νΌ μμ
μ μ€λ λλ₯Ό μμ±
executor.execute(() -> {
// νμ΄λ¨Έμ μ€λΉ μλ£ μ νΈλ₯Ό 보λ
ready.countDown();
try {
// λͺ¨λ μμ
μ μ€λ λκ° μ€λΉλ λκΉμ§ κΈ°λ€λ¦Ό
start.await();
action.run();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
// νμ΄λ¨Έμ μμ
μλ£ μ νΈλ₯Ό 보λ
done.countDown();
}
});
}
ready.await(); // λͺ¨λ μμ
μκ° μ€λΉλ λκΉμ§ κΈ°λ€λ¦Ό
long startNanos = System.nanoTime();
start.countDown(); // μμ
μλ€μ κΉ¨μ
done.await(); // λͺ¨λ μμ
μκ° μμ
μ μλ£ν λκΉμ§ κΈ°λ€λ¦Ό
return System.nanoTime() - startNanos;
// nanoTimeμ΄ currentTimesMillisλ³΄λ€ λ μ ννκ³ , μμ€ν
μ μ€μκ° μκ³ μκ° λ³΄μ μ μν₯μ λ°μ§ μμ
}
}waitμ notify
waitμ notifyLast updated