day110-cache-distributed lock-Redisson-locking countdownlatch test

 1.Countdownlatch concept

What is he and what is it for?

In fact, it is equivalent to a counter. For example, before you run a certain thread, you must have 10 threads running before you can run it. Then you can use this thing to ensure that,

In fact, what scenes are there, such as when your computer is turned on, the bios checks whether the memory and graphics card is normal, what interface settings are loaded, the operating system is loaded, and so on. You can enter the desktop after the threads are running.

2. Simulation scene

Here we simulate a running race. There are ten people in the race, and the race is over after all of them have run or the time exceeds ten minutes.

Upload code

    @RequestMapping({"/gameProcess"})
    @ResponseBody
    public String gameProcess(){
        //模拟跑步比赛
        System.out.println("发令枪------啪");

        RCountDownLatch countDownLatch = redissonClient.getCountDownLatch("countDownLatch");
        countDownLatch.trySetCount(10);
        try {
            countDownLatch.await(10, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "比赛结束";
    }

    @RequestMapping({"/finish/{id}"})
    @ResponseBody
    public String finish(@PathVariable String id){
        RCountDownLatch countDownLatch = redissonClient.getCountDownLatch("countDownLatch");
        countDownLatch.countDown();
        return id+"选手跑完了";
    }

Test and analysis:

(1). Start the game and wait after the referee gives the order

(2) Competitors start to finish

. . . Others continue to finish until the last classmate

Okay, I have finished running from 1 to 10. It seems that I don’t have to work overtime for ten minutes. The referee stopped waiting and announced the end of the game.

 

 

Guess you like

Origin blog.csdn.net/JavaCoder_juejue/article/details/115058503