0

If I run like this, the callback block will not run so future.get() will block forever:

FutureTask<Long> futureTask = new FutureTask<>(System::currentTimeMillis);

boolean login = CameraSDk.login(ip, port, userName, password, 
       (lLoginID, pBuf, RevLen, EncodeType, CmdSerial, dwUser) -> {
    //do something
    futureTask.run();
});

try {
    Long timestamp = futureTask.get();
    System.out.println("timestamp = " + timestamp);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
    e.printStackTrace();
}

But if I use this:

Long timestamp = futureTask.get(1, TimeUnit.SECONDS);

The callback block will be called when futuretask.get() times out.

I don't know what happened, is this a dead lock or something else?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
King K
  • 1
  • `FutureTask` with a single argument is documented to take a `Callable` argument. I don't see how your `Long` parameter implements the `Callable` interface. What is the `call()` method implementation associated with this `FutureTask`? – Daniel Widdis Nov 22 '21 at 04:09
  • `Callable callable = new Callable() { @Override public Long call() throws Exception { return System.currentTimeMillis(); } }; ` `FutureTask futureTask = new FutureTask<>(callable); ` – King K Nov 22 '21 at 11:41

0 Answers0