2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.lang
.ref
.PhantomReference
;
22 import java
.lang
.ref
.ReferenceQueue
;
25 * Wait until an object has become unreachable.
27 * <p>Instances of this class will typically be used as either:</p>
29 * SomeType o = new SomeType(...);
31 * WaitUnreachable u = new WaitUnreachable(o);
33 * u.waitUnreachable();
37 * WaitUnreachable u = new WaitUnreachable(new SomeType(...));
38 * ... // use "(SomeType) u.get()"
39 * u.waitUnreachable();
42 public final class WaitUnreachable
{
44 * Creates a new waiter.
46 * @param obj the object on which to wait
48 public WaitUnreachable(Object obj
) {
50 queue
= new ReferenceQueue
<Object
>();
51 ref
= new PhantomReference
<Object
>(obj
, queue
);
55 * Gets the object on which to wait.
57 * @return the object on which to wait, or <code>null</code> if
58 * <code>waitUnreachable</code> has already been called
60 public synchronized Object
get() {
65 * Starts waiting for the object to become unreachable.
67 * <p>This blocks the current thread until the object has become
70 * <p>Actually, this method waits until the JVM has <em>detected</em> that
71 * the object has become unreachable. This is not deterministic, but this
72 * methods makes a best effort to cause the JVM to eventually detect the
73 * situation. With a typical JVM, this should suffice.</p>
75 public void waitUnreachable() {
79 System
.out
.println("waiting for gc");
80 while (queue
.poll() == null) {
82 System
.runFinalization();
87 * Ensures that an object will be finalized as soon as possible.
89 * <p>This does not block the current thread. Instead, a new thread is
90 * spawned that busy waits for the given object to become unreachable.</p>
92 * <p>This method cannot guarantee that the given object is eventually
93 * finalized, but it makes a best effort. With a typical JVM, this should
96 * @param obj the object of which to ensure finalization
98 public static void ensureFinalization(final Object obj
) {
99 final class WaitThread
extends Thread
{
100 private WaitThread(Object obj
) {
101 super("ensureFinalization");
102 unreachable
= new WaitUnreachable(obj
);
107 unreachable
.waitUnreachable();
110 private final WaitUnreachable unreachable
;
112 new WaitThread(obj
).start();
116 private final ReferenceQueue
<Object
> queue
;
117 @SuppressWarnings("unused")
118 private final PhantomReference
<Object
> ref
;