提问人:stonar96 提问时间:11/13/2023 最后编辑:stonar96 更新时间:11/13/2023 访问量:85
JVM 能否在进入 try-block 之前轮询异步异常?
Can the JVM poll for asynchronous exceptions just before entering a try-block?
问:
最近我读到了 JLS 11.1.3。异步异常。我想知道是否有任何关于轮询异步异常(并抛出它们)可以或不能发生的任何规范。例如,请考虑以下代码:
try {
statement1();
} finally {
// comment
try {
statement2();
} finally {
statement3();
}
}
如果异步异常被轮询并抛出任何异常,其他语句仍将被尝试执行。但是,是否可以有一个有效的 JVM 实现,即在 处输入 -块之前轮询异步异常,以便尝试执行剩余的两个语句中的任何一个?statement#()
try
// comment
JLS 表示,异步异常“可能在程序执行的任何时候发生”,但轮询是否需要语句,如果有,究竟什么算作语句?
我还编写了一些测试代码,通过调用已弃用的方法触发异步异常。Thread#stop()
private static volatile boolean hot = false;
private static volatile boolean state1 = false;
private static volatile boolean state2 = false;
private static volatile boolean state3 = false;
private static volatile boolean dummyOperationState = false;
public static void main(String[] args) {
for (int i = 1;; i++) {
hot = false;
state1 = false;
state2 = false;
state3 = false;
Thread thread = new Thread(() -> {
try {
while (true) {
try {
state1 = true;
dummyOperationState = !dummyOperationState;
} finally {
// dummyOperationState = !dummyOperationState;
try {
state2 = true;
dummyOperationState = !dummyOperationState;
} finally {
state3 = true;
dummyOperationState = !dummyOperationState;
}
}
// Replace the above code by this:
/* state1 = true;
dummyOperationState = !dummyOperationState;
state2 = true;
dummyOperationState = !dummyOperationState;
state3 = true;
dummyOperationState = !dummyOperationState; */
state1 = false;
state2 = false;
state3 = false;
hot = true;
}
} finally {
hot = false;
}
});
thread.setDaemon(true);
thread.start();
while (!hot);
// Add some randomness.
int random = (int) (Math.random() * 100.);
for (int j = 0; j < random; j++) {
Math.sin(Math.sqrt(j * 2.));
}
thread.stop();
while (hot);
if (state1 && !state2 && !state3) {
System.out.println("run: " + i);
System.out.println("state1: " + state1);
System.out.println("state2: " + state2);
System.out.println("state3: " + state3);
break;
}
if (i % 1000 == 0) {
System.out.println(i);
}
}
}
这些语句并不是真正必需的,但编译器似乎在没有它们的情况下进行了太多的优化,无法演示注释掉的代码。如上所示的程序在我运行时不会终止。我的问题是,这是否由任何有效的JVM实现保证(前提是除了触发的例外之外没有发生其他或进一步的异常)?dummyOperationState = !dummyOperationState;
ThreadDeath
当我将第一条注释转换为语句时,可以在那里轮询异步异常,程序可能会终止,这正是我的测试在运行几次后发生的情况。注释掉的代码只是演示了没有 --blocks 的情况,它也会在某个时候终止。// dummyOperationState = !dummyOperationState;
try
finally
答: 暂无答案
评论