1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.lib.util;
29 
30 import java.util.LinkedList;
31 
32 /**
33    Helper class to asynchronously execute finalize methods.
34 
35    Current JVMs seem not to be robust against long-running finalize methods, in
36    that such long-running finalize methods may lead to OutOfMemoryErrors.  This
37    class mitigates the problem by asynchronously shifting the bodies of
38    potentially long-running finalize methods into an extra thread.  Classes that
39    make use of this in their finalize methods are the proxies used in the
40    intra-process JNI UNO bridge and the inter-process Java URP UNO bridge (where
41    in both cases finalizers lead to synchronous UNO release calls).
42 
43    If JVMs are getting more mature and should no longer have problems with
44    long-running finalize mehtods, this class could be removed again.
45 */
46 public final class AsynchronousFinalizer {
47     /**
48        Add a job to be executed asynchronously.
49 
50        The run method of the given job is called exactly once.  If it terminates
51        abnormally by throwing any Throwable, that is ignored.
52 
53        @param job represents the body of some finalize method; must not be null.
54     */
55     public static void add(Job job) {
56         synchronized (queue) {
57             boolean first = queue.isEmpty();
58             queue.add(job);
59             if (first) {
60                 queue.notify();
61             }
62         }
63     }
64 
65     /**
66        An interface to represent bodies of finalize methods.
67 
68        Similar to Runnable, except that the run method may throw any Throwable
69        (which is effectively ignored by AsynchronousFinalizer.add, similar to
70        any Throwables raised by finalize being ignored).
71     */
72     public interface Job {
73         void run() throws Throwable;
74     }
75 
76     private static final LinkedList queue = new LinkedList();
77 
78     static {
79         Thread t = new Thread() {
80                 public void run() {
81                     for (;;) {
82                         Job j;
83                         synchronized (queue) {
84                             while (queue.isEmpty()) {
85                                 try {
86                                     queue.wait();
87                                 } catch (InterruptedException e) {}
88                             }
89                             j = (Job) queue.remove(0);
90                         }
91                         try {
92                             j.run();
93                         } catch (Throwable e) {}
94                     }
95                 }
96             };
97         t.setDaemon(true);
98         t.start();
99     }
100 
101     private AsynchronousFinalizer() {}
102 }
103