Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package finalizerdemo;
- public class A
- {
- private int index;
- private A other;
- public A(int index)
- {
- this.index = index;
- }
- public void setOther(A other)
- {
- this.other = other;
- }
- @Override
- protected void finalize() throws Throwable
- {
- System.out.print("A(" + index + ")#finalize");
- finalizerAssert(other != null); // the other object must be still available
- finalizerAssert(index + 1 == other.index || index - 1 == other.index); // the two indexes must be exactly 1 from each other
- System.out.print(" other(" + other.index + ")");
- other = null; // just to make sure if we did something wrong, we would get an assertion error
- System.out.println(" finalizer finished");
- }
- private static void finalizerAssert(boolean success)
- {
- if(!success)
- {
- new AssertionError().printStackTrace();
- System.exit(1);
- }
- }
- public static void main(String[] args)
- {
- for(int i = 0; true; i += 2)
- {
- A a = new A(i);
- A b = new A(i + 1);
- a.setOther(b);
- b.setOther(a);
- System.out.print('.'); // this is just an indicator that GC doesn't run after every iteration
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement