0

I'm trying to find a way in Java 6 to wait for a result from a method that reads the database waiting for some information.

The actual code is very similar to this:

Result result = getSomeResult();
Long maxTries = 10;
Long sleepTimeInMs = 2000; // 10 tries and 2000 ms for each call is 20000 ms of timeout in total

int tries = 0;
while (tries < maxTries && (result == null || result.isProcessing())) {
    Thread.sleep(sleepTimeInMs);
    result = getSomeResult();
    tries++;
}

In the code above, I'm not just waiting for a result; I'm waiting for a not null result and in a specific condition (not processing).

Thanks!

Dherik
  • 17,757
  • 11
  • 115
  • 164
  • packt that in a Future Promise – ΦXocę 웃 Пepeúpa ツ Jun 13 '17 at 19:03
  • I guess you will find the answer here: https://stackoverflow.com/questions/4817933/what-is-the-equivalent-to-a-javascript-setinterval-settimeout-in-android-java or here: https://stackoverflow.com/questions/26311470/what-is-the-equivalent-of-javascript-settimeout-in-java – edward_wong Jun 13 '17 at 19:03
  • http://www.deadcoderising.com/java8-writing-asynchronous-code-with-completablefuture/ – GriffeyDog Jun 13 '17 at 19:04
  • @ΦXocę웃Пepeúpaツ Future wait for the method return some result, but I'm waiting for a result that is "not null" and is not "processing". – Dherik Jun 13 '17 at 19:05
  • @GriffeyDog, sorry, I'm using Java 6. I will edit the question to add this information. – Dherik Jun 13 '17 at 19:06

1 Answers1

0

this is a way to make delay to run a part of code, you can make a delay on it

        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
          //the action you want to do after reading database
        }
    }, delay_time);

or you can make your method for reading database "async"

Meikiem
  • 1,876
  • 2
  • 11
  • 19