Java中与.NET的System.Action等效的内容是什么?

3
在.NET下,System.Action提供以下方法:
  1. Invoke()
  2. BeginInvoke(AsyncCallback, object)
  3. EndInvoke(IAsyncresult)
请问我如何获取以上方法的Java等效方法?
谢谢。

3
与C#不同,Java没有委托。因此我认为在Java中没有与System.Action等价的东西。 - Cheng Chen
1个回答

2

Java 7开始,您可以使用Executors框架。您可以找到一些示例:这里

其中一个示例(从上面的链接复制而来 - 示例是针对Java 8的,因为使用了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);

调用等效:

int result = task.call();

BeginInvoke的等效方法:

Future<Integer> future = executor.submit(task);

EndInvoke的等价物:

int result = future.get();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接