Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is Future<void>?

Tags:

java

vert.x

I have following code snippet.

import io.vertx.core.Future;
public void start(Future<void> fut){

  /*
  some code
  */
  fut.complete()
}


Why does Future used here?

like image 849
Satish_Mhetre Avatar asked May 28 '18 13:05

Satish_Mhetre


People also ask

What does Future void mean in flutter?

If a future doesn't produce a usable value, then the future's type is Future<void> . A future can be in one of two states: uncompleted or completed.

What does void mean in Java?

Definition and Usage The void keyword specifies that a method should not have a return value.

What is a Future class?

Simply put, the Future class represents a future result of an asynchronous computation. This result will eventually appear in the Future after the processing is complete. Let's see how to write methods that create and return a Future instance.

How do you return a void in darts?

In a large variety of languages void as return type is used to indicate that a function doesn't return anything. Dart allows returning null in functions with void return type but it also allow using return; without specifying any value. To have a consistent way you should not return null and only use an empty return.


1 Answers

Future<Void> is a future result of an execution that returns no value.

That would be typically the result of invoking the run method of a Runnable.

The normal void call looks like (see r.run()):

Runnable r = () -> System.out.println("running");
r.run();

When such a call is done asynchronously, such as via an executor service or a completable future, it turns into a future:

Future<Void> future = CompletableService.runAsync(r);

It's just a future of an execution that returns no result. This "future" contains information about the execution, even though it has no "return" value (such as what Future<Object> would have).

You can get information about the asynchronous execution. Some examples of information it hods:

boolean finished = future.isDone(); //Check if the async execution has completed
future.get(10L, TimeUnit.SECONDS); //Wait for completion with timeout
future.cancel(true); //cancel the execution

java.lang.Void is a reference type for void (a placeholder that doesn't get instantiated). So you can look at Future<Void> the same way you look at Future<Object>, just keeping in mind what you know about void not returning any value.

You can read more about these types here:

  • Future
  • Void
  • CompletableFuture
like image 70
ernest_k Avatar answered Oct 17 '22 21:10

ernest_k