bump product version to 6.4.0.3
[LibreOffice.git] / jurt / com / sun / star / lib / util / AsynchronousFinalizer.java
blob588b8fe388f6d765c93a26c2e169233f5f25a18a
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 package com.sun.star.lib.util;
22 import java.util.LinkedList;
24 /**
25 * Helper class to asynchronously execute finalize methods.
27 * <p>Current JVMs seem not to be robust against long-running finalize methods,
28 * in that such long-running finalize methods may lead to OutOfMemoryErrors.
29 * This class mitigates the problem by asynchronously shifting the bodies of
30 * potentially long-running finalize methods into an extra thread. Classes that
31 * make use of this in their finalize methods are the proxies used in the
32 * intra-process JNI UNO bridge and the inter-process Java URP UNO bridge (where
33 * in both cases finalizers lead to synchronous UNO release calls).</p>
35 * <p>Irrespective whether JVMs are getting more mature and should no longer
36 * have problems with long-running finalize methods, at least the JNI UNO bridge
37 * needs some way to stop finalization of proxies (to C++ objects) well before
38 * process exit, as provided by drain().</p>
40 public final class AsynchronousFinalizer {
41 public AsynchronousFinalizer() {
42 thread = new Thread("AsynchronousFinalizer") {
43 @Override
44 public void run() {
45 for (;;) {
46 Job j;
47 synchronized (queue) {
48 for (;;) {
49 if (done) {
50 return;
52 if (!queue.isEmpty()) {
53 break;
55 try {
56 queue.wait();
57 } catch (InterruptedException e) {
58 return;
61 j = queue.remove(0);
63 try {
64 j.run();
65 } catch (Throwable e) {}
69 thread.start();
72 /**
73 * Add a job to be executed asynchronously.
75 * <p>The run method of the given job is called exactly once. If it terminates
76 * abnormally by throwing any Throwable, that is ignored.</p>
78 * @param job represents the body of some finalize method; must not be null.
80 public void add(Job job) {
81 synchronized (queue) {
82 boolean first = queue.isEmpty();
83 queue.add(job);
84 if (first) {
85 queue.notify();
90 public void drain() throws InterruptedException {
91 synchronized (queue) {
92 done = true;
93 queue.notify();
95 // tdf#123481 Only join if we are not in our own thread, else we have a deadlock
96 if (Thread.currentThread() != thread)
97 thread.join();
101 * An interface to represent bodies of finalize methods.
103 * Similar to <code>Runnable</code>, except that the run method may throw any
104 * <code>Throwable</code> (which is effectively ignored by
105 * <code>AsynchronousFinalizer.add</code>, similar to any <code>Throwables</code>
106 * raised by finalize being ignored).
108 public interface Job {
109 void run() throws Throwable;
112 private final LinkedList<Job> queue = new LinkedList<Job>();
113 private final Thread thread;
114 private boolean done = false;
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */