update dev300-m58
[ooovba.git] / qadevOOo / runner / util / WaitUnreachable.java
blob0e2fa1292ea80336e739b13b7646bf0499318140
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WaitUnreachable.java,v $
10 * $Revision: 1.5 $
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 ************************************************************************/
31 package util;
33 import java.lang.ref.PhantomReference;
34 import java.lang.ref.ReferenceQueue;
36 /**
37 * Wait until an object has become unreachable.
39 * <p>Instances of this class will typically be used as either:</p>
40 * <pre>
41 * SomeType o = new SomeType(...);
42 * ... // use "o"
43 * WaitUnreachable u = new WaitUnreachable(o);
44 * o = null;
45 * u.waitUnreachable();
46 * </pre>
47 * <p>or:</p>
48 * <pre>
49 * WaitUnreachable u = new WaitUnreachable(new SomeType(...));
50 * ... // use "(SomeType) u.get()"
51 * u.waitUnreachable();
52 * </pre>
54 public final class WaitUnreachable {
55 /**
56 * Creates a new waiter.
58 * @param obj the object on which to wait
60 public WaitUnreachable(Object obj) {
61 this.obj = obj;
62 queue = new ReferenceQueue();
63 ref = new PhantomReference(obj, queue);
66 /**
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() {
73 return obj;
76 /**
77 * Starts waiting for the object to become unreachable.
79 * <p>This blocks the current thread until the object has become
80 * unreachable.</p>
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() {
88 synchronized (this) {
89 obj = null;
91 System.out.println("waiting for gc");
92 while (queue.poll() == null) {
93 System.gc();
94 System.runFinalization();
95 byte[] dummy = new byte[1024];
99 /**
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
107 * suffice.</p>
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);
118 public void run() {
119 unreachable.waitUnreachable();
122 private final WaitUnreachable unreachable;
124 new WaitThread(obj).start();
127 private Object obj;
128 private final ReferenceQueue queue;
129 private final PhantomReference ref;