Reputation: 360
In .NET under System.Action
the following methods are available.
Invoke()
BeginInvoke(AsyncCallback, object)
EndInvoke(IAsyncresult)
How do I get the Java equivalent for the above methods.
Thanks in advance.
Upvotes: 3
Views: 1565
Reputation: 4543
Starting form Java 7
you can use Executors
framework. You can find a couple of examples: here
One of the examples shown there (copied "as is" from the link above - an example is for Java 8
because of lambda
):
Callable<Integer> task = () -> {
try {
TimeUnit.SECONDS.sleep(1);
return 123;
}
catch (InterruptedException e) {
throw new IllegalStateException("task interrupted", e);
}
};
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<Integer> future = executor.submit(task);
Invoke equivalent:
int result = task.call();
BeginInvoke equivalent:
Future<Integer> future = executor.submit(task);
EndInvoke equivalent:
int result = future.get();
Upvotes: 2