1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.lib.util;
25 
26 import java.util.LinkedList;
27 
28 /**
29    Helper class to asynchronously execute finalize methods.
30 
31    Current JVMs seem not to be robust against long-running finalize methods, in
32    that such long-running finalize methods may lead to OutOfMemoryErrors.  This
33    class mitigates the problem by asynchronously shifting the bodies of
34    potentially long-running finalize methods into an extra thread.  Classes that
35    make use of this in their finalize methods are the proxies used in the
36    intra-process JNI UNO bridge and the inter-process Java URP UNO bridge (where
37    in both cases finalizers lead to synchronous UNO release calls).
38 
39    If JVMs are getting more mature and should no longer have problems with
40    long-running finalize mehtods, this class could be removed again.
41 */
42 public final class AsynchronousFinalizer {
43     /**
44        Add a job to be executed asynchronously.
45 
46        The run method of the given job is called exactly once.  If it terminates
47        abnormally by throwing any Throwable, that is ignored.
48 
49        @param job represents the body of some finalize method; must not be null.
50     */
add(Job job)51     public static void add(Job job) {
52         synchronized (queue) {
53             boolean first = queue.isEmpty();
54             queue.add(job);
55             if (first) {
56                 queue.notify();
57             }
58         }
59     }
60 
61     /**
62        An interface to represent bodies of finalize methods.
63 
64        Similar to Runnable, except that the run method may throw any Throwable
65        (which is effectively ignored by AsynchronousFinalizer.add, similar to
66        any Throwables raised by finalize being ignored).
67     */
68     public interface Job {
run()69         void run() throws Throwable;
70     }
71 
72     private static final LinkedList queue = new LinkedList();
73 
74     static {
75         Thread t = new Thread() {
76                 public void run() {
77                     for (;;) {
78                         Job j;
79                         synchronized (queue) {
80                             while (queue.isEmpty()) {
81                                 try {
82                                     queue.wait();
83                                 } catch (InterruptedException e) {}
84                             }
85                             j = (Job) queue.remove(0);
86                         }
87                         try {
88                             j.run();
89                         } catch (Throwable e) {}
90                     }
91                 }
92             };
93         t.setDaemon(true);
t.start()94         t.start();
95     }
96 
AsynchronousFinalizer()97     private AsynchronousFinalizer() {}
98 }
99