Skip to main content
Post Closed as "Duplicate" by Sotirios Delimanolis java
Source Link
Mandroid
  • 7.4k
  • 15
  • 85
  • 174

Async API using Future never completes

I try to make an API aysnchronous as:

Future<Integer> fASync(int x) {
        return new FutureTask(() -> {
            try {
                Thread.sleep(new Random().nextInt(1, 3) * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return x * x;
        });
    }

..then I try to use it:

Future<Integer> asyncCall = fASync(x);
asyncCall .get();

But this never completes and call just blocks.

Is this not correct way of making your API asynchronous?