可以阻塞/等待已经存在的异步函数吗?

huangapple go评论65阅读模式
英文:

Is it possible to block/wait an already existing asynchronous function?

问题

SomeLibrary lib = new SomeLibrary();
lib.doSomethingAsync(); // 我得到的库中的某个函数,它会异步地打印1-5
System.out.println("Done");

// 输出结果
// Done
// 1
// 2
// 3
// 4
// 5

我想明确指出我并未编写doSomethingAsync()函数,我无法对其进行更改。我想找到一种方法来阻塞这个异步函数,在打印数字1到5之后再打印Done,因为正如你所看到的,Done会立即被打印出来。在Java中是否有实现这样的方法呢?

英文:
SomeLibrary lib = new SomeLibrary();
lib.doSomethingAsync(); // some function from a library I got and what it does is print 1-5 asynchronously
System.out.println("Done");

// output
// Done
// 1
// 2
// 3
// 4
// 5

I want to be clear that I didn't make the doSomethingAsync() function and it's out of my ability to change it. I want to find a way to block this async function and print Done after the numbers 1 to 5 because as you see Done is being instantly printed. Is there a way to do this in Java?

答案1

得分: 1

你可以按照以下方式使用CountDownLatch

    final CountDownLatch wait = new CountDownLatch(1);
    SomeLibrary lib = new SomeLibrary(wait);
    lib.doSomethingAsync(); // 我得到的某个库中的某个函数,它会异步打印1-5
    // 注意,在doSomethingAsync中,你必须在返回之前调用wait.countDown()
    wait.await(); // 在这里等待,直到wait.countDown()被调用。
    System.out.println("Done");

SomeLibrary构造函数中:

    private CountDownLatch wait;
    public ScannerTest(CountDownLatch _wait) {
        this.wait = _wait;
    }

doSomethingAsync()方法中:

    public void doSomethingAsync(){
       // TODO:执行某些操作
       ...
       this.wait.countDown();
       return;
    }
英文:

You can use CountDownLatch as follow:

final CountDownLatch wait = new CountDownLatch(1);
SomeLibrary lib = new SomeLibrary(wait);
lib.doSomethingAsync(); // some function from a library I got and what it does is print 1-5 asynchronously
//NOTE in the doSomethingAsync, you must call wait.countDown() before return
wait.await(); //-> it wait in here until wait.countDown() is called.
System.out.println("Done");

In Constructor SomeLibrary :

private CountDownLatch  wait;
public ScannerTest(CountDownLatch  _wait) {
    this.wait = _wait;
}

In method doSomethingAsync():

public void doSomethingAsync(){
   //TODO something
   ...
   this.wait.countDown();
   return;
}

答案2

得分: 1

这在标准库中有几种实现方式:

完成回调

客户端通常可以提供一个函数,在异步任务完成后调用该函数。此函数通常接收有关已完成工作的某些信息作为输入。

Future.get()

异步函数返回Future以进行客户端同步。您可以在此处阅读更多有关它们的信息。

请检查您希望调用的方法是否提供了这些选项中的任何一个(也许是重载版本?)。图书馆中通常包括某些业务逻辑的同步和异步版本,因此您也可以搜索一下是否存在。

英文:

This is achieved in a couple of ways in standard libraries :-

Completion Callback

Clients can often provider function to be invoked after the async task is complete. This function usually receives some information regarding the work done as it's input.

Future.get()

Async functions return Future for client synchronization. You can read more about them here.

Do check if any of these options are available (perhaps, an overloaded version ?_ in the method you wish to invoke. It is not too uncommon for libraries to include both sync and async version of some business logic so you could search for that too.

huangapple
  • 本文由 发表于 2020年10月4日 16:56:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64192773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定