Implement a blocking function call in Java

前端 未结 3 700
-上瘾入骨i
-上瘾入骨i 2020-12-30 09:18

What is the recommended / best way to implement a blocking function call in Java, that can be later unblocked by a call from another thread?

Basically I want to have

3条回答
  •  失恋的感觉
    2020-12-30 09:44

    You can use a CountDownLatch.

    latch = new CountDownLatch(1);
    

    To block, call:

    latch.await();
    

    To unblock, call:

    latch.countDown();
    

提交回复
热议问题