暂时只能想到把他绑定到一个固定的 “Foo” 实例上。。。。
import java.lang.invoke.*;
public class Foo {
public void bar(Object o) {
}
public static void main(String[] args) throws Throwable {
MethodHandles.Lookup l = MethodHandles.lookup();
MethodType t = MethodType.methodType(void.class, Object.class);
Foo foo = new Foo();
MethodHandle mh = l.findVirtual(Foo.class, "bar", t).bindTo(foo);
long current = System.currentTimeMillis();
for (int i = 1; i <= 2_000_000_000; i++) {
if (i % 100_000_000 == 0) {
long temp = System.currentTimeMillis();
System.out.println(temp - current);
current = temp;
}
mh.invokeExact(new Object());
}
}
}
展开