Java example source code file (GcFinalization.java)
This example Java source code file (GcFinalization.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
/*
* Copyright (C) 2011 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.testing;
import static java.util.concurrent.TimeUnit.SECONDS;
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtIncompatible;
import com.google.j2objc.annotations.J2ObjCIncompatible;
import java.lang.ref.WeakReference;
import java.util.Locale;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;
/**
* Testing utilities relating to garbage collection finalization.
*
* <p>Use this class to test code triggered by finalization, that is, one of the
* following actions taken by the java garbage collection system:
*
* <ul>
* <li>invoking the {@code finalize} methods of unreachable objects
* <li>clearing weak references to unreachable referents
* <li>enqueuing weak references to unreachable referents in their reference queue
* </ul>
*
* <p>This class uses (possibly repeated) invocations of {@link java.lang.System#gc()} to cause
* finalization to happen. However, a call to {@code System.gc()} is specified to be no more
* than a hint, so this technique may fail at the whim of the JDK implementation, for example if
* a user specified the JVM flag {@code -XX:+DisableExplicitGC}. But in practice, it works very
* well for ordinary tests.
*
* <p>Failure of the expected event to occur within an implementation-defined "reasonable" time
* period or an interrupt while waiting for the expected event will result in a {@link
* RuntimeException}.
*
* <p>Here's an example that tests a {@code finalize} method:
*
* <pre> {@code
* final CountDownLatch latch = new CountDownLatch(1);
* Object x = new MyClass() {
* ...
* protected void finalize() { latch.countDown(); ... }
* };
* x = null; // Hint to the JIT that x is stack-unreachable
* GcFinalization.await(latch);}</pre>
*
* <p>Here's an example that uses a user-defined finalization predicate:
*
* <pre> {@code
* final WeakHashMap<Object, Object> map = new WeakHashMap
Other Java examples (source code examples)
Here is a short list of links related to this Java GcFinalization.java source code file: