没有看到对 Callable.call() 方法的任何调用,但 call() 方法中的代码仍然被执行,call() 方法在哪里被调用?

Don't see any calls to the Callable.call() method but the code inside call() method still got executed, where was the call() method been called?

我是 Java 的新手,我正在尝试学习接口 ScheduledExecutorService。我在网上看到下面的代码。

我没有看到对 Callable.call() 方法的任何调用,但 call() 方法中的代码仍然得到执行。哪里调用了call()方法?

ScheduledExecutorService ses1 = Executors.newScheduledThreadPool(5);

ScheduledFuture sf1 = ses1.schedule(new Callable() {
    @Override
    public Object call() throws Exception {
        System.out.println("executed!");
        return "called";
    }
},
        2,
        TimeUnit.SECONDS);

ses1.shutdown();

输出:

executed!

如果你查看调度方法API,你会发现你的调用方法将在 2 秒后执行。

ScheduledFuture<?> schedule(Runnable command,
                      long delay,
                      TimeUnit unit)

创建并执行在给定延迟后启用的一次性操作。

ScheduledExecutorService 中称为 schedule() 的方法根据 documentation 执行以下操作:

Creates and executes a one-shot action that becomes enabled after the given delay.

第一个参数可以是 CallableRunnable。在任何一种情况下,当超时到期时,ScheduledExecutorService 将调用 Callable's call() 方法或 Runnable 的 run() 方法。

这正是执行程序服务所做的。