简体   繁体   English

暂停执行方法,直到回调完成

[英]Pause execution of a method until callback is finished

I am fairly new to Java and extremely new to concurrency. 我对Java相当陌生,对并发性则非常陌生。 However, I have worked with C# for a while. 但是,我使用C#已有一段时间了。 It doesn't really matter, but for the sake of example, I am trying to pull data off a table on server. 其实并不重要,但是为了举例说明,我试图从服务器上的表中提取数据。 I want method to wait until data is completely pulled. 我想要方法等到数据被完全拉出。 In C#, we have async-await pattern which can be used like this: 在C#中,我们有一个async-await模式,可以这样使用:

private async Task<List<ToDoItem>> PullItems ()
{
    var newRemoteItems = await (from p in remoteTable select p).ToListAsync();
    return newRemoteItems;
}

I am trying to have similar effect in Java. 我试图在Java中产生类似的效果。 Here is the exact code I'm trying to port (Look inside SynchronizeAsync method.)! 是我要移植的确切代码(查看SynchronizeAsync方法。)! However, Java Azure SDK works with callbacks. 但是,Java Azure SDK可与回调一起使用。 So, I have a few options: 因此,我有几种选择:

Use wait and notify pattern. 使用waitnotify模式。 Following code doesn't work since I don't understand what I'm doing. 由于我不了解自己在做什么,因此遵循以下代码无效。

final List<TEntity> newRemoteItems = new ArrayList<TEntity>();
synchronized( this ) {
    remoteTable.where().field("lastSynchronized").gt(currentTimeStamp)
    .execute(new TableQueryCallback<TEntity>() {
          public void onCompleted(List<TEntity> result, 
                                  int count, 
                                  Exception exception,
                                  ServiceFilterResponse response) {
              if (exception == null) {
                  newRemoteItems.clear();
                  for (TEntity item: result) {
                      newRemoteItems.add(item);
                      }
                  }
              } 
          });
}
this.wait();
//DO SOME OTHER STUFF

My other option is to move DO SOME OTHER STUFF right inside the callback's if(exception == null) block. 我的另一个选择是将DO SOME OTHER STUFF移到回调的if(exception == null)块内。 However, this would result in my whole method logic chopped off into the pieces, disturbing the continuous flow. 但是,这将导致我的整个方法逻辑被分解成碎片,从而干扰了连续流程。 I don't really like this approach. 我不太喜欢这种方法。

Now, here are questions: 现在,这里有问题:

  1. What is recommended way of doing this? 建议这样做的方法是什么? I am completing the tutorial on Java concurrency at Oracle. 我正在Oracle上完成有关Java并发的教程。 Still, clueless. 不过,一无所知。 Almost everywhere I read, it is recommended to use higher level stuff rather than wait and notify . 在我阅读的几乎所有地方,建议使用更高级别的内容,而不要waitnotify

  2. What is wrong with my wait and notify ? 我的waitnotify什么问题?

  3. My implementation blocks the main thread and it's considered a bad practice. 我的实现阻塞了主线程,这被认为是不好的做法。 But what else can I do? 但是我还能做什么? I must wait for the server to respond! 我必须等待服务器响应! Also, doesn't C# await block the main thread? 此外,不C# await阻塞主线程? How is that not a bad thing? 那怎么不是一件坏事?

Either put DO SOME OTHER STUFF into callback, or declare a semaphore , and call semaphore.release in the callback and call semaphore.aquire where you want to wait. 要么把DO SOME OTHER STUFF进入回调,或宣布一个信号 ,并调用semaphore.release回调,并呼吁semaphore.aquire要等待。 Remove synchronized(this) and this.wait. 删除synced(this)和this.wait。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM