Arun Prasad
Arun Prasad

Reputation: 360

Java equivalent for System.Action in .NET

In .NET under System.Action the following methods are available.

  1. Invoke()
  2. BeginInvoke(AsyncCallback, object)
  3. EndInvoke(IAsyncresult)

How do I get the Java equivalent for the above methods.

Thanks in advance.

Upvotes: 3

Views: 1565

Answers (1)

Denis Itskovich
Denis Itskovich

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

Related Questions