
com.xiongyingqi.util.WeakReferenceMonitor Maven / Gradle / Ivy
/*
* Copyright 2002-2012 the original author or 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.xiongyingqi.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
/**
* Track references to arbitrary objects using proxy and weak references. To
* monitor a handle, one should call {@link #monitor(Object, com.xiongyingqi.util.WeakReferenceMonitor.ReleaseListener)},
* with the given handle object usually being a holder that uses the target
* object underneath, and the release listener performing cleanup of the
* target object once the handle is not strongly referenced anymore.
*
* When a given handle becomes weakly reachable, the specified listener
* will be called by a background thread. This thread will only be started
* lazily and will be stopped once no handles are registered for monitoring
* anymore, to be restarted if further handles are added.
*
* Thanks to Tomasz Wysocki for the suggestion and the original
* implementation of this class!
*
* @author Colin Sampaleanu
* @author Juergen Hoeller
* @see #monitor
* @since 1.2
*/
public class WeakReferenceMonitor {
private static final Log logger = LogFactory.getLog(WeakReferenceMonitor.class);
// Queue receiving reachability events
private static final ReferenceQueue
© 2015 - 2025 Weber Informatics LLC | Privacy Policy