1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WaitUnreachable.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import java
.lang
.ref
.PhantomReference
;
34 import java
.lang
.ref
.ReferenceQueue
;
37 * Wait until an object has become unreachable.
39 * <p>Instances of this class will typically be used as either:</p>
41 * SomeType o = new SomeType(...);
43 * WaitUnreachable u = new WaitUnreachable(o);
45 * u.waitUnreachable();
49 * WaitUnreachable u = new WaitUnreachable(new SomeType(...));
50 * ... // use "(SomeType) u.get()"
51 * u.waitUnreachable();
54 public final class WaitUnreachable
{
56 * Creates a new waiter.
58 * @param obj the object on which to wait
60 public WaitUnreachable(Object obj
) {
62 queue
= new ReferenceQueue();
63 ref
= new PhantomReference(obj
, queue
);
67 * Gets the object on which to wait.
69 * @return the object on which to wait, or <code>null</code> if
70 * <code>waitUnreachable</code> has already been called
72 public synchronized Object
get() {
77 * Starts waiting for the object to become unreachable.
79 * <p>This blocks the current thread until the object has become
82 * <p>Actually, this method waits until the JVM has <em>detected</em> that
83 * the object has become unreachable. This is not deterministic, but this
84 * methods makes a best effort to cause the JVM to eventually detect the
85 * situation. With a typical JVM, this should suffice.</p>
87 public void waitUnreachable() {
91 System
.out
.println("waiting for gc");
92 while (queue
.poll() == null) {
94 System
.runFinalization();
95 byte[] dummy
= new byte[1024];
100 * Ensures that an object will be finalized as soon as possible.
102 * <p>This does not block the current thread. Instead, a new thread is
103 * spawned that busy waits for the given object to become unreachable.</p>
105 * <p>This method cannot guarantee that the given object is eventually
106 * finalized, but it makes a best effort. With a typical JVM, this should
109 * @param obj the object of which to ensure finalization
111 public static void ensureFinalization(final Object obj
) {
112 final class WaitThread
extends Thread
{
113 public WaitThread(Object obj
) {
114 super("ensureFinalization");
115 unreachable
= new WaitUnreachable(obj
);
119 unreachable
.waitUnreachable();
122 private final WaitUnreachable unreachable
;
124 new WaitThread(obj
).start();
128 private final ReferenceQueue queue
;
129 private final PhantomReference ref
;