给定一个 Runnable 对象,例如
public class Test implements Runnable {
@Override
public void run() {
int x = 2;
int y = 6;
// Snip more code
int w = x - 1;
int z = x * y;
}
}
我希望能够执行确切数量的操作,例如
Test t = new Test();
Executor.execute(t, 100); // Arbitrary unit of operations
这样,如果我第一次执行此操作会达到:
int w = x - 1;
任何其他时间我用相同的参数调用该方法都会导致执行到同一点。
我环顾四周,看不到任何合适的东西(例如,据我所知,ScheduledThreadPoolExecutor 不会工作)。
我是否必须转移到字节码级别才能完成这项工作?从我读过的内容来看,JIT 也可能在这里引起问题。