백앤드 이야기/JAVA&Spring

[JAVA] 동시성 테스트

한희성 2023. 12. 14.
반응형

 // Given
        log.info("makeReservation 동시성 테스트 준비");
        int numberOfThreads = 5;
        ExecutorService service = Executors.newFixedThreadPool(numberOfThreads);
        CountDownLatch latch = new CountDownLatch(numberOfThreads);

        // When
        log.info("makeReservation 동시성 테스트 진행");

        service.execute(() -> {
            System.out.println(LocalDateTime.now());
            log.info("approvalTxId 검사-1 Response : {}", redisService.keyLock(repeatCheckRequest));
            latch.countDown();
        });
        service.execute(() -> {
            System.out.println(LocalDateTime.now());
            log.info("approvalTxId 검사-1 Response : {}", redisService.keyLock(repeatCheckRequest));
            latch.countDown();
        });
        service.execute(() -> {
            System.out.println(LocalDateTime.now());
            log.info("approvalTxId 검사-1 Response : {}", redisService.keyLock(repeatCheckRequest));
            latch.countDown();
        });
        service.execute(() -> {
            System.out.println(LocalDateTime.now());
            log.info("approvalTxId 검사-1 Response : {}", redisService.keyLock(repeatCheckRequest));
            latch.countDown();
        });
        service.execute(() -> {
            System.out.println(LocalDateTime.now());
            log.info("approvalTxId 검사-1 Response : {}", redisService.keyLock(repeatCheckRequest));
            latch.countDown();
        });

        latch.await();



  int numberOfThreads = 5;
            ExecutorService service = Executors.newFixedThreadPool(numberOfThreads);
            CountDownLatch latch = new CountDownLatch(numberOfThreads);

            service.execute(() -> {
                System.out.println("1번 스레드 : " + LocalDateTime.now());
                try {
                    checkApprovalTxId(redisKey);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                latch.countDown();
            });
            service.execute(() -> {
                System.out.println("2번 스레드 : " + LocalDateTime.now());
                try {
                    checkApprovalTxId(redisKey);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                latch.countDown();
            });

반응형

댓글

💲 추천 글