在Java中实现阻塞函数调用

发布于 2024-12-09 03:20:30 字数 472 浏览 0 评论 0原文

在 Java 中实现阻塞函数调用的推荐/最佳方法是什么,稍后可以通过另一个线程的调用来解除阻塞?

基本上我想在一个对象上有两个方法,其中第一个调用会阻塞任何调用线程,直到另一个线程运行第二个方法:

public class Blocker {

  /* Any thread that calls this function will get blocked */
  public static SomeResultObject blockingCall() {
     // ...      
  }

  /* when this function is called all blocked threads will continue */
  public void unblockAll() {
     // ...
  }

} 

顺便说一句,其意图不仅仅是获得阻塞行为,而是编写一个阻塞直到将来的方法可以计算所需结果的点。

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 two methods on an object, where the first call blocks any calling thread until the second method is run by another thread:

public class Blocker {

  /* Any thread that calls this function will get blocked */
  public static SomeResultObject blockingCall() {
     // ...      
  }

  /* when this function is called all blocked threads will continue */
  public void unblockAll() {
     // ...
  }

} 

The intention BTW is not just to get blocking behaviour, but to write a method that blocks until some future point when it is possible to compute the required result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄居者 2024-12-16 03:20:30

您可以使用 CountDownLatch

latch = new CountDownLatch(1);

要阻止,请调用:

latch.await();

要取消阻止,请调用:

latch.countDown();

You can use a CountDownLatch.

latch = new CountDownLatch(1);

To block, call:

latch.await();

To unblock, call:

latch.countDown();
澜川若宁 2024-12-16 03:20:30

如果您正在等待特定对象,可以使用一个线程调用 myObject.wait(),然后使用 myObject.notify()唤醒它>myObject.notifyAll()。您可能需要位于 synchronized 块内:

class Example {

    List list = new ArrayList();

    // Wait for the list to have an item and return it
    public Object getFromList() {
        synchronized(list) {
            // Do this inside a while loop -- wait() is
            // not guaranteed to only return if the condition
            // is satisfied -- it can return any time
            while(list.empty()) {
                // Wait until list.notify() is called
                // Note: The lock will be released until
                //       this call returns.
                list.wait();
            }
            return list.remove(0);
        }
    }

    // Add an object to the list and wake up
    // anyone waiting
    public void addToList(Object item) {
        synchronized(list) {
            list.add(item);
            // Wake up anything blocking on list.wait() 
            // Note that we know that only one waiting
            // call can complete (since we only added one
            // item to process. If we wanted to wake them
            // all up, we'd use list.notifyAll()
            list.notify();
        }
    }
}

If you're waiting on a specific object, you can call myObject.wait() with one thread, and then wake it up with myObject.notify() or myObject.notifyAll(). You may need to be inside a synchronized block:

class Example {

    List list = new ArrayList();

    // Wait for the list to have an item and return it
    public Object getFromList() {
        synchronized(list) {
            // Do this inside a while loop -- wait() is
            // not guaranteed to only return if the condition
            // is satisfied -- it can return any time
            while(list.empty()) {
                // Wait until list.notify() is called
                // Note: The lock will be released until
                //       this call returns.
                list.wait();
            }
            return list.remove(0);
        }
    }

    // Add an object to the list and wake up
    // anyone waiting
    public void addToList(Object item) {
        synchronized(list) {
            list.add(item);
            // Wake up anything blocking on list.wait() 
            // Note that we know that only one waiting
            // call can complete (since we only added one
            // item to process. If we wanted to wake them
            // all up, we'd use list.notifyAll()
            list.notify();
        }
    }
}
高速公鹿 2024-12-16 03:20:30

有几种不同的方法和原语可用,但最合适的听起来像 CyclicBarrierCountDownLatch

There are a couple of different approaches and primitives available, but the most appropriate sounds like a CyclicBarrier or a CountDownLatch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文