Pause execution of a method until callback is finished

akshay2000

I am fairly new to Java and extremely new to concurrency. However, I have worked with C# for a while. 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:

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. Here is the exact code I'm trying to port (Look inside SynchronizeAsync method.)! However, Java Azure SDK works with callbacks. So, I have a few options:

Use wait and notify pattern. 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. 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. Still, clueless. Almost everywhere I read, it is recommended to use higher level stuff rather than wait and notify.

  2. What is wrong with my wait and notify?

  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? How is that not a bad thing?

Alexei Kaigorodov

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. Remove synchronized(this) and this.wait.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pause execution until button press

From Java

Javascript Pause Loop Until function is finished

From Dev

Pause Perl script until an outside program is finished

From Dev

Sequelize wait until loop finished with callback

From Dev

Is this a correct way to pause thread until callback?

From Dev

Android - Pause execution until sound has played

From Dev

Pause function execution until another function returns

From Dev

Wait until a callback ends to continue execution in Angular

From Dev

Hold on callback function execution until promise is completed

From Dev

Command line does not wait until the exe execution is finished

From Dev

How to halt execution in Vuejs until get request is finished

From Dev

How to make JS wait until protocol execution finished

From Dev

Delay emission of observable until the execution of the current Angular tick has finished

From Dev

Pause execution until user input received from tkinter window

From Dev

Why the main does not wait until the async method has finished?

From Dev

How to hold the thread execution until asynchronous thread return callback

From Dev

nodejs: force callback execution if method hangs

From Dev

Pause a thread until a method and all the threads inside finish their work?

From Dev

How to pause the program until we get the result of asynchoronous method?

From Dev

How to wait until a callback is finished to return from ES6 Proxy

From Dev

Block execution until children called via MPI_Comm_spawn have finished

From Dev

Is there some method/function to pause python without stopping the current execution?

From Dev

Wait until function is finished

From Dev

Javascript/jQuery - How do you pause the execution of a loop until a form is submitted?

From Dev

Android thread finished callback

From Dev

How to wait until find method has finished before doing further processing in Ember Model

From Dev

How can I delay the ng-init method until my scope Variable finished loading?

From Dev

pause and resume jmeter execution

From Dev

Pause Execution in Python

Related Related

  1. 1

    Pause execution until button press

  2. 2

    Javascript Pause Loop Until function is finished

  3. 3

    Pause Perl script until an outside program is finished

  4. 4

    Sequelize wait until loop finished with callback

  5. 5

    Is this a correct way to pause thread until callback?

  6. 6

    Android - Pause execution until sound has played

  7. 7

    Pause function execution until another function returns

  8. 8

    Wait until a callback ends to continue execution in Angular

  9. 9

    Hold on callback function execution until promise is completed

  10. 10

    Command line does not wait until the exe execution is finished

  11. 11

    How to halt execution in Vuejs until get request is finished

  12. 12

    How to make JS wait until protocol execution finished

  13. 13

    Delay emission of observable until the execution of the current Angular tick has finished

  14. 14

    Pause execution until user input received from tkinter window

  15. 15

    Why the main does not wait until the async method has finished?

  16. 16

    How to hold the thread execution until asynchronous thread return callback

  17. 17

    nodejs: force callback execution if method hangs

  18. 18

    Pause a thread until a method and all the threads inside finish their work?

  19. 19

    How to pause the program until we get the result of asynchoronous method?

  20. 20

    How to wait until a callback is finished to return from ES6 Proxy

  21. 21

    Block execution until children called via MPI_Comm_spawn have finished

  22. 22

    Is there some method/function to pause python without stopping the current execution?

  23. 23

    Wait until function is finished

  24. 24

    Javascript/jQuery - How do you pause the execution of a loop until a form is submitted?

  25. 25

    Android thread finished callback

  26. 26

    How to wait until find method has finished before doing further processing in Ember Model

  27. 27

    How can I delay the ng-init method until my scope Variable finished loading?

  28. 28

    pause and resume jmeter execution

  29. 29

    Pause Execution in Python

HotTag

Archive