Closed
Description
The following program intermittently prints "Wait on monitor o! Should not be interrupted!" -> ~10/1000
import java.lang.System.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.lang.reflect.Method;
import java.util.Arrays;
public static void main(String[] args) {
final Object o = new Object();
final AtomicBoolean allInterruptsHaveBeenSignalled = new AtomicBoolean(false);
final Thread waiter = new Thread(()->{
synchronized (o) {
try {
o.wait();
} catch (InterruptedException e) {
System.out.println("Waiter has been interrupted!");
}
}
while (!allInterruptsHaveBeenSignalled.get()) {
try {
System.out.println("Wait for interrupters to be gone!");
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Interrupted while sleeping!");
}
}
final Thread thisThread = Thread.currentThread();
Thread.interrupted();
try {
if (!thisThread.isInterrupted()) {
Method method = Thread.class.getDeclaredMethod("isInterruptedImpl");
method.setAccessible(true);
boolean r = (boolean) method.invoke(thisThread);
if (r) {
System.out.println("Interrupt event in omrthread still set, but Thread.interrupted is false!");
System.out.println("Just for safety, clear interrupt flag again!");
Thread.interrupted();
System.out.println("Wait on monitor o! Should not be interrupted!");
synchronized (o) {
try {
o.wait();
} catch (InterruptedException e) {
System.out.println("FAILURE! No interrupt could have been signalled!");
System.exit(1);
}
}
}
}
}
catch (Exception e) {
System.out.println("Unexpected: " + e.toString());
}
})
{
public void interrupt() {
System.out.println("Interrupt:\n\tinterrupter: "
+ Arrays.toString(Thread.currentThread().getStackTrace())
+ "\n\twaiter: "
+ Arrays.toString(getStackTrace()));
super.interrupt();
}
};
final Thread interrupter1 = new Thread(()->{
waiter.interrupt();
});
final Thread interrupter2 = new Thread(()->{
waiter.interrupt();
});
waiter.start();
interrupter1.start();
interrupter2.start();
try {
interr
592A
upter1.join();
interrupter2.join();
System.out.println("Interrupters have been terminated!");
allInterruptsHaveBeenSignalled.set(true);
waiter.join();
} catch (InterruptedException e) {
System.out.println("Interrupted while joining: " + e.toString());
}
System.out.println("Done!");
}