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 .
19 package com
.sun
.star
.lib
.util
;
21 import java
.util
.LinkedList
;
24 * Helper class to asynchronously execute finalize methods.
26 * <p>Current JVMs seem not to be robust against long-running finalize methods,
27 * in that such long-running finalize methods may lead to OutOfMemoryErrors.
28 * This class mitigates the problem by asynchronously shifting the bodies of
29 * potentially long-running finalize methods into an extra thread. Classes that
30 * make use of this in their finalize methods are the proxies used in the
31 * intra-process JNI UNO bridge and the inter-process Java URP UNO bridge (where
32 * in both cases finalizers lead to synchronous UNO release calls).</p>
34 * <p>Irrespective whether JVMs are getting more mature and should no longer
35 * have problems with long-running finalize methods, at least the JNI UNO bridge
36 * needs some way to stop finalization of proxies (to C++ objects) well before
37 * process exit, as provided by drain().</p>
39 public final class AsynchronousFinalizer
{
40 public AsynchronousFinalizer() {
41 thread
= new Thread("AsynchronousFinalizer") {
46 synchronized (queue
) {
51 if (!queue
.isEmpty()) {
56 } catch (InterruptedException e
) {
64 } catch (Throwable e
) {}
72 * Add a job to be executed asynchronously.
74 * <p>The run method of the given job is called exactly once. If it terminates
75 * abnormally by throwing any Throwable, that is ignored.</p>
77 * @param job represents the body of some finalize method; must not be null.
79 public void add(Job job
) {
80 synchronized (queue
) {
81 boolean first
= queue
.isEmpty();
89 public void drain() throws InterruptedException
{
90 synchronized (queue
) {
98 * An interface to represent bodies of finalize methods.
100 * Similar to <code>Runnable</code>, except that the run method may throw any
101 * <code>Throwable</code> (which is effectively ignored by
102 * <code>AsynchronousFinalizer.add</code>, similar to any <code>Throwables</code>
103 * raised by finalize being ignored).
105 public interface Job
{
106 void run() throws Throwable
;
109 private final LinkedList
<Job
> queue
= new LinkedList
<Job
>();
110 private final Thread thread
;
111 private boolean done
= false;