01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2006, GeoTools Project Managment Committee (PMC)
05: * (C) 2006, Institut de Recherche pour le Développement
06: * (C) 2006, Geomatys
07: *
08: * This library is free software; you can redistribute it and/or
09: * modify it under the terms of the GNU Lesser General Public
10: * License as published by the Free Software Foundation;
11: * version 2.1 of the License.
12: *
13: * This library is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16: * Lesser General Public License for more details.
17: */
18: package org.geotools.image.io.stream;
19:
20: // J2SE dependencies
21: import java.util.Timer;
22: import java.util.TimerTask;
23:
24: /**
25: * Stops execution of {@link System#runFinalization} when a timeout is reach. This timer is
26: * used in order to prevent system freeze that occurs sometime when waiting for finalization
27: * in {@link UrlInputSpi#createInputStreamInstance}. If a {@code finalize()} method is blocked
28: * in an I/O operation upon an interruptible channel, the channel will be closed and the finalize
29: * method will receive a {@link java.nio.channels.ClosedByInterruptException}.
30: * See {@link Thread#interrupt} javadoc for more details.
31: * <p>
32: * Most of the time, interruptions are never performed.
33: *
34: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/main/java/org/geotools/image/io/stream/FinalizationStopper.java $
35: * @version $Id: FinalizationStopper.java 25595 2007-05-20 13:58:56Z desruisseaux $
36: * @author Martin Desruisseaux
37: */
38: final class FinalizationStopper extends TimerTask {
39: /**
40: * Timer for stopping execution of {@link System#runFinalization}.
41: *
42: * @todo Uncomment the String argument when we will be allowed to compile for J2SE 1.5.
43: */
44: private static final Timer TIMER = new Timer(
45: /*"Finalization stopper",*/true);
46:
47: /**
48: * The processus to stop.
49: */
50: private final Thread toStop;
51:
52: /**
53: * {@code true} if the thread has been interrupted at least once.
54: */
55: volatile boolean interrupted;
56:
57: /**
58: * Stops a new task which will stops current thread after the specified timeout. The
59: * interruption will be repeated until {@link UrlInputSpi#createInputStreamInstance}
60: * stop them.
61: */
62: FinalizationStopper(final long timeout) {
63: this .toStop = Thread.currentThread();
64: TIMER.schedule(this , timeout, timeout);
65: }
66:
67: /**
68: * Stops the thread.
69: */
70: public void run() {
71: interrupted = true;
72: toStop.interrupt();
73: }
74: }
|