Update ooo320-m1
[ooovba.git] / jurt / com / sun / star / lib / util / AsynchronousFinalizer.java
blobe80074d891f7378a6f4de0d706ba33d3adf8f81c
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: AsynchronousFinalizer.java,v $
10 * $Revision: 1.3 $
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 com.sun.star.lib.util;
33 import java.util.LinkedList;
35 /**
36 Helper class to asynchronously execute finalize methods.
38 Current JVMs seem not to be robust against long-running finalize methods, in
39 that such long-running finalize methods may lead to OutOfMemoryErrors. This
40 class mitigates the problem by asynchronously shifting the bodies of
41 potentially long-running finalize methods into an extra thread. Classes that
42 make use of this in their finalize methods are the proxies used in the
43 intra-process JNI UNO bridge and the inter-process Java URP UNO bridge (where
44 in both cases finalizers lead to synchronous UNO release calls).
46 If JVMs are getting more mature and should no longer have problems with
47 long-running finalize mehtods, this class could be removed again.
49 public final class AsynchronousFinalizer {
50 /**
51 Add a job to be executed asynchronously.
53 The run method of the given job is called exactly once. If it terminates
54 abnormally by throwing any Throwable, that is ignored.
56 @param job represents the body of some finalize method; must not be null.
58 public static void add(Job job) {
59 synchronized (queue) {
60 boolean first = queue.isEmpty();
61 queue.add(job);
62 if (first) {
63 queue.notify();
68 /**
69 An interface to represent bodies of finalize methods.
71 Similar to Runnable, except that the run method may throw any Throwable
72 (which is effectively ignored by AsynchronousFinalizer.add, similar to
73 any Throwables raised by finalize being ignored).
75 public interface Job {
76 void run() throws Throwable;
79 private static final LinkedList queue = new LinkedList();
81 static {
82 Thread t = new Thread() {
83 public void run() {
84 for (;;) {
85 Job j;
86 synchronized (queue) {
87 while (queue.isEmpty()) {
88 try {
89 queue.wait();
90 } catch (InterruptedException e) {}
92 j = (Job) queue.remove(0);
94 try {
95 j.run();
96 } catch (Throwable e) {}
100 t.setDaemon(true);
101 t.start();
104 private AsynchronousFinalizer() {}