mirror of
https://github.com/corda/corda.git
synced 2025-06-17 14:48:16 +00:00
run java finalizers in a separate thread to guarantee no application locks are held when doing so
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
public class Finalizers {
|
||||
private static final Object lock = new Object();
|
||||
private static boolean finalized = false;
|
||||
|
||||
private static void expect(boolean v) {
|
||||
@ -6,15 +7,21 @@ public class Finalizers {
|
||||
}
|
||||
|
||||
protected void finalize() {
|
||||
finalized = true;
|
||||
synchronized (lock) {
|
||||
finalized = true;
|
||||
lock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) throws Exception {
|
||||
new Finalizers();
|
||||
|
||||
expect(! finalized);
|
||||
|
||||
System.gc();
|
||||
synchronized (lock) {
|
||||
System.gc();
|
||||
lock.wait(5000);
|
||||
}
|
||||
|
||||
expect(finalized);
|
||||
|
||||
@ -24,7 +31,10 @@ public class Finalizers {
|
||||
|
||||
expect(! finalized);
|
||||
|
||||
System.gc();
|
||||
synchronized (lock) {
|
||||
System.gc();
|
||||
lock.wait(5000);
|
||||
}
|
||||
|
||||
expect(finalized);
|
||||
}
|
||||
|
Reference in New Issue
Block a user