μ€λμ μ€μ μ μ·¨μ νΉκ°μ΄ μμκ³ , μ€νμλ νλ‘μ νΈ μ§νμ νμλ€.
λ©μ¬μμ λμ€μ νμ₯? λμ΄ λμ€μ μ μ€λͺ ν΄μ£Όμ ¨λλ°, λλ΅μ μΈ λ΄μ©μ μ΄λ¬λ€.
μν°λ ν΅κ³λ₯Ό 보면 κΈ°λ³Έ 60κ° μ΄λ ₯μλ₯Ό μμ±ν΄μΌ 1κ°κ° λΆλλ€ μμΌλ©°, λμ²΄λ‘ λ°±μλ κ°λ°μλ κ·Έλλ§ λ§μ΄ λ½λλ°, μ μ μ λ½λ λΉλμκ° νμ°ν μ€μλ€κ³ νλ€. μν°λλ μ‘μ½λ¦¬μ, μ¬λμΈκ³Ό κ°μ μ·¨μ ν¬νΈ μ¬μ΄νΈ μ΄λ€.
β‘ μμ μλ리μ€λ μ΄λ¬νλ€. Aμ μ μ Bμ μ κ° μλ€κ³ μ‘΄μ¬νκ³ , λ±λ‘λ μν 10κ°μ λν΄ μ₯λ°κ΅¬λμ μνμ κ°κ° 1κ°μ© λ΄κ²λλ©΄, μνμ μ¬κ³ μλμ΄ 8κ°κ° λμ΄μΌ νλλ° 9κ°κ° λλ€λ κ² μ΄μλ€.
μλμ μ½λλ κ°λ¨ν λμμ±μ ν
μ€νΈ ν μμλ ν
μ€νΈ μ½λμ
λλ€.
μνμ κ°―μκ° 20κ°μΈ κ°μμ§ μ¬λ£λ₯Ό λ±λ‘νμκ³ , 10κ°μ λ©ν°μ°λ λλ₯Ό μμ±νμ¬, 20λ²μ μ₯λ°κ΅¬λμ λ΄λ νλμ νμμ΅λλ€.
@Test
public void λμμ_20κ°_μμ²() throws InterruptedException {
product = Product.builder().productName("κ°μμ§ μ¬λ£")
.price("25_000")
.content("μ ν΅κΈ°ν 1λ
λ¨μ μ¬λ£μ
λλ€.")
.count(20)
.build();
productRepository.save(product);
int threadCount = 20;
// 10κ°μ μ°λ λ μμ±
ExecutorService executorService = Executors.newFixedThreadPool(10);
CountDownLatch latch = new CountDownLatch(threadCount);
// 50λ²μ λ°λ³΅
for (int i = 0; i < threadCount; i++) {
executorService.submit(() -> {
try {
product.removeStock(1);
System.out.println(product.getCount());
} finally {
latch.countDown();
}
});
}
latch.await();
Product findProduct = productRepository.findById(1L).orElseThrow();
System.out.println(findProduct.getCount());
Assertions.assertThat(0).isEqualTo(findProduct.getCount());
}
ν΄λΉ ν μ€νΈ μ½λλ₯Ό μ€ν ν΄λ³΄λ©΄,
μν μ κ³ κ° 20κ°μ΄κ³ , 20λ²μ μ₯λ°κ΅¬λμ λ΄λ νλμ νμ¬, μ¬κ³ κ° 0κ°κ° λμ΄μΌ νμ§λ§, ν μ€νΈ μ½λκ° μ€ν¨ νλ κ²μ μμ μκ³ , μνμ μ¬κ³ κ°―μκ° μ΄μνκ² μΆλ ₯ λλ κ²μ μ μ μμ΅λλ€.
μ΄λ¬ν λ¬Έμ μ μ "λμμ" λ°μ΄ν° μμμ λ³κ²½νλ €κ³ ν λ λ°μ ν©λλ€.
μ¬κΈ°μ μ΄μ λΉκ΄μ λ½κ³Ό λκ΄μ λ½μ΄λΌλ κ°λ μ΄ λμ€λλ°, μ΄ λΆλΆμ λν΄μλ νλ‘μ νΈμ μ μ©ν΄λ³΄λ©° νμ€ν μμμλ μ 리 ν΄λλ©΄ μ’μ κ² κ°λ€.