使用 Callable 时出现奇怪的调用堆栈

Strange call stack when using Callable

从使用 Executors 和 callable 的调用中转储调用堆栈时,我看到了奇怪的结果。来自可调用对象的方法在调用堆栈中出现了两次。

pool-1-thread-1@454, prio=5, in group 'main', status: 'RUNNING'
      at com.test.tracked.ACallable.call(ACallable.java:15)
      at com.test.tracked.ACallable.call(ACallable.java:9)
      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
      at java.util.concurrent.FutureTask.run(FutureTask.java:166)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      at java.lang.Thread.run(Thread.java:722)

可以看到,方法ACallable在stack:The中出现了两次第9行是ACallable的声明class第15行是方法签名:

package com.test.tracked;

import java.util.concurrent.Callable;

/**
 *  A callable test class.
 *  
 */
public final class ACallable
    implements Callable<String> {
  private final String who;

  @Override
  public String call() throws Exception {
    Thread.dumpStack();
    return "Hello "+who+" from callable";
  }

  public ACallable(String who) {
    this.who = who;
  }

}

'main' 线程:

main@1, prio=5, in group 'main', status: 'WAIT'
      at sun.misc.Unsafe.park(Unsafe.java:-1)
      at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
      at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:834)
      at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:994)
      at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1303)
      at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:248)
      at java.util.concurrent.FutureTask.get(FutureTask.java:111)
      at com.test.tracked.AsynchronousCall.callFuture(AsynchronousCall.java:26)
      at com.test.Main.main(Main.java:21)

调用可调用对象的代码:

package com.test.tracked;

import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeoutException;



/**
 * Aynchronous call
 */
public class AsynchronousCall implements Closeable {

  private final ExecutorService executorService;


  public AsynchronousCall() {
    executorService = Executors.newSingleThreadExecutor();
  }

  public String callFuture(String who) throws InterruptedException, ExecutionException, TimeoutException {
    Thread.dumpStack();
    String ret = executorService.submit(new ACallable(who)).get();
    System.out.println("callFuture from " + getClass().getName() + " return " + ret);
    return ret;
  }

  @Override
  public void close() throws IOException {
    executorService.shutdownNow();
  }
}

编译器添加了一个合成桥接方法来支持泛型。所以

@Override
public String call() throws Exception {
    Thread.dumpStack();
    return "Hello "+who+" from callable";
}

在编译的.class文件中确实是两种方法

// actual method
public Object call() throws Exception {
    return call(); // the other call
} 

// your implementation
public String call() throws Exception {
    Thread.dumpStack();
    return "Hello "+who+" from callable";
}

(请注意,这在源代码中是不可能的,因为这两种方法具有相同的签名。)

这在其他问题和答案中有进一步解释:

  • Java Generics - Bridge method?

这是编译器添加桥接方法的结果。

在 JVM 中,方法没有协变 return 类型。尝试调用 Object call() 不会 调用 String call() - 如果没有 Object call() 方法,它将抛出 NoSuchMethodError。所以编译器添加了一个bridge方法类似于:

public Object call() throws Exception {
    return call(); // the call() method that returns String, not this one
}

为什么会有 Object call() 方法?由于 通用类型擦除 - 字节码不使用通用类型。在字节码中,Callable 看起来像这样:

interface Callable {
    Object call() throws Exception;
}

ACallable 看起来像这样:(这是无效的 Java 源代码,因为它包含两个具有相同名称和参数的方法)

class ACallable implements Callable {
    private final String who;

    // does NOT override the method in Callable due to the different return type
    public String call() throws Exception {
        Thread.dumpStack();
        return "Hello "+who+" from callable";
    }

    // overrides the method in Callable
    public Object call() throws Exception {
        return call(); // the version of call() that returns String
    }
}

你可以在生成的class文件中通过运行javap看到这个。