How to make sure a task starts its execution only after a previous asynchronous task has completed i


I have two tasks - task1() and task2(). task1() fetches a value from the internet and adds it with a int variable named num. Variable num is accessible to both task1() and task2(). task2() is just supposed to print the value of variable num.

But the problem here is that before the value is fetched from the internet ,the num variable gets printed.

int num = 0; public static void main(String[] args) { task1(); task2(); } public static void task1() { int fetchedValue; rootRef.addListenerForSingleValueEvent(new ValueEventListener() { public void onDataChange(DataSnapshot snapshot) { fetchedValue = Integer.parseInt(snapshot.getValue().toString()); num = num + fetchedValue ; } public void onCancelled(FirebaseError arg0) { System.out.println("cancelled"); } }); num = num + fetchedValue; } public static void task2() { System.out.println("Updated number : "+num); }


You could use a boolean to determine if the value has been fetched.

This boolean is accessible from both tasks. task1 changes boolean to true if it has completed fetching. task2 checks in a loop whether fetching has finished and prints num if num has been fetched.


  • Passing firebase data from one activity to another via clickable listview
  • How to use the heightForRowAtIndexPath method?
  • How to compare multiple column with multiple field array in Select query?
  • Fetching Data From A Specific div id Using PHP [duplicate]
  • Use a foreach loop instead of while with myslqli_fetch_array()
  • Sending rails errors to rspec output
  • PWA with multiple pages
  • Why doesnt this Java loop in a thread work?
  • Display java JPanel in a JFrame
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • How to Cache Real-time Data?
  • how to adjust image in a panel in Java swing?
  • How to access EntityManager inside Entity class in EJB3
  • how to do an event when i swipe from fragment to the other
  • Spring Data JPA custom method causing PropertyReferenceException
  • JFileChooser in front of fullscreen Swing application
  • HTML download movie download link
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Join two tables and save into third-sql
  • Knitr HTML Loop - Some HTML output, some R output
  • sending/ receiving email in Java
  • How to model a transition system with SPIN
  • Eraser for UIBezierPath
  • ORA-29908: missing primary invocation for ancillary operator
  • align graphs with different xlab
  • Return words with double consecutive letters
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • Suggestions to manage Login/Logout transitions
  • JTable with a ScrollPane misbehaving
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Reading document lines to the user (python)
  • failed to connect to specific WiFi in android programmatically
  • Converting MP3 duration time
  • Python/Django TangoWithDjango Models and Databases
  • How can I use threading to 'tick' a timer to be accessed by other threads?