1

I need to do some async method. And not wait until it executing. I try with Future but it not help.

Future<Boolean> future = executorService.submit(new MyCallable());
LOGGER.info("onFailedLogonLimitAttempt: before");
                        if (future.get().booleanValue()) {
                          // some code here
                        }
                        LOGGER.info("onFailedLogonLimitAttempt: after");

public class MyCallable implements Callable<Boolean> {

        // The call() method is called in order to execute the asynchronous task.
        @Override
        public Boolean call() throws Exception {
            try {
                LOGGER.info("MyCallable: start");
                Thread.sleep(10000L);
                LOGGER.info("MyCallable: alarm_cleanup_stop 10 seconds");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            return true;
        }
    }

But here logs:

2022-03-24 17:28:55.436 INFO  [SomeClass:http-nio-172.19.5.163-8091-exec-10] onFailedLogonLimitAttempt: before
2022-03-24 17:28:55.436 INFO  [SomeClass:pool-24-thread-1] MyCallable: start
...
...
...
2022-03-24 17:29:05.437 INFO  [SomeClass:pool-24-thread-1] MyCallable: alarm_cleanup_stop 10 seconds
2022-03-24 17:29:05.441 INFO  [SomeClass:http-nio-172.19.5.163-8091-exec-10] onFailedLogonLimitAttempt: after

As you can see the log print "onFailedLogonLimitAttempt: after" is call AFTER 10 seconds. But I need log to print imedaitly after "onFailedLogonLimitAttempt: before". To not wait unit async method call is finish.

Alexei
  • 14,350
  • 37
  • 121
  • 240
  • I think what you're looking for is a callback. There are several ways to go about, but that's what I suggest you research, then you can get rid of your blocking future.get() call. – Ryan Mar 24 '22 at 15:46
  • 1
    Err, I don't get your problem. You call `get()` on your future, and as documented ... that call blocks until the result is available. So the real answer here is: don't just put down some code. You have to understand what the library classes/methods that you are using are doing. – GhostCat Mar 24 '22 at 15:50
  • @Ryan I need the main thread to run async task and not wait result. But after e.g. 30 second the async task must return boolean. Is it possible? – Alexei Mar 24 '22 at 15:51
  • 1
    Then do not call future.get() on the main thread. You are explicitly pulling the brake, and then you keep wondering why the car gets slower. *Wanting* to go faster doesn't help when you are actively pulling the brakes ‍♂️ – GhostCat Mar 24 '22 at 15:52
  • Yes, but you have to organize your code. With a callback, you pass a lambda or another class to your callable to be called when your callable completes. By doing that you don't need to wait for the callable to complete, the class or operation that needs to information will be notified when the data is ready. – Ryan Mar 24 '22 at 15:54

1 Answers1

3

The main thread is blocked on the call to future.get() until the task completes. Remove this call and your second log statement will print immediately.

That addresses what you asked exactly. But I doubt it is what you want.

The purpose of submitting an asynchronous task is to permit the main thread to carry on immediately with other work. If the main thread requires the result of the task to proceed, the task is not a candidate for performing asynchronously.

Instead, add the processing of the result to the asynchronous task itself. Then the main thread does not have to wait for the result.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • @erikson I need the main thread to run async task and not wait result. But after e.g. 30 second the async task must return boolean. Is it possible? – Alexei Mar 24 '22 at 15:49
  • He told you what you need to do: register a callback that gets invoked from your MyCallable instance when it is done. And when you need some sort of timeout, then use the version of get() that ... takes a timeout. Seriously, I think the answer for you is to step back and read a good tutorial on how to use Futures in Java. – GhostCat Mar 24 '22 at 15:54
  • @Alexei If the result takes 30 seconds to compute, how can you act on it without waiting? If a loaf takes 30 minutes to bake, asking someone else to put it in the oven doesn't magically allow you to eat it immediately. That is analogous to what you seem to expect here. – erickson Mar 24 '22 at 16:00