묻고 답해요
129만명의 커뮤니티!! 함께 토론해봐요.
인프런 TOP Writers
-
미해결재고시스템으로 알아보는 동시성이슈 해결방법
비관적 락 적용을 해도 동시성 테스트 시 실패합니다...
StockRepositorypublic interface StockRepository extends JpaRepository<Stock, Long> { @Lock(value = LockModeType.PESSIMISTIC_WRITE) @Query("select s from Stock s where s.id = :id") Stock findByIdWithPessimisticLock(@Param("id") Long id); }StockService@Service @RequiredArgsConstructor public class StockService { private final StockRepository stockRepository; @Transactional public Long decrease(Long id, Long quantity) { Stock stock = stockRepository.findByIdWithPessimisticLock(id); stock.decrease(quantity); stockRepository.saveAndFlush(stock); return stock.getQuantity(); } }StockServiceTest@SpringBootTest class PessimisticLockStockServiceTest { @Autowired private StockService service; @Autowired private StockRepository stockRepository; @BeforeEach public void before() { stockRepository.saveAndFlush(new Stock(1L, 100L)); } @AfterEach public void after() { stockRepository.deleteAll(); } @Test @DisplayName("비관적 락을 사용해 재고 감소 동시성 요청이 완료된다.") void decrease() throws InterruptedException { // given int threadCnt = 100; ExecutorService executorService = Executors.newFixedThreadPool(32); CountDownLatch latch = new CountDownLatch(threadCnt); // when for (int i = 0; i < threadCnt; i++) { executorService.submit(() -> { try { service.decrease(1L, 1L); } finally { latch.countDown(); } }); } latch.await(); // then Stock stock = stockRepository.findById(1L).orElseThrow(); assertThat(stock.getQuantity()).isZero(); } }해당 테스트를 돌리면 실패하고 순차적으로 재고가 감소되지 않고 수정 손실이 발생합니다. 아무리 찾아봐도 코드는 제대로 짠 것 같은데 무엇이 잘못 되었을까요??
-
미해결운영체제 공룡책 강의
교수님 질문있습니다!
안녕하세요 교수님 양질의 강의 잘 듣고 있습니다. 감사합니다! 멀티스레딩에 대해 궁금한 점이 있어 질문남깁니다. 1. 멀티프로세싱 대비 멀티스레딩의 장점 중 하나가, context switching의 비용이 적다는 것이라고 알고 있습니다. 그런데 context switching이란 명령어를 실행하기 위한 pc, stack pointer 등을 바꾸는 과정으로 알고 있는데, 스레드의 경우에도 pc, stack pointer는 각각 다르지 않습니까?? 그럼 어차피 스레드도 pc, stack pointer를 바꿀텐데 왜 context switching의 비용이 싼 건지 궁금합니다! 2. 서버는 멀티프로세스를 쓰는게 좋은지 멀티스레딩을 쓰는게 좋은지도 궁금합니다! 다시 한번 좋은 강의 감사드립니다.