【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“CleanupQueue.java”一文,供大家参考学习!
/* -*- mode: Java; fill-column: 72 -*-
* The contents of this file are subject to the ClickBlocks Public
* License Version 1.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.clickblocks.org
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied, including, but not limited to, the implied warranties of
* merchantability, fitness for a particular purpose and
* non-infringement. See the License for the specific language
* governing rights and limitations under the License.
*
* ClickBlocks, the ClickBlocks logo and combinations thereof are
* trademarks of ClickBlocks, LLC in the United States and other
* countries.
*
* The Initial Developer of the Original Code is ClickBlocks, LLC.
* Portions created by ClickBlocks, LLC are Copyright (C) 2000.
* All Rights Reserved.
*/
/*
* $Log: CleanupQueue.java,v $
* Revision 1.2 2000/11/27 04:37:13 mgrand
* Code complete.
*
* Revision 1.1 2000/11/25 09:49:41 mgrand
* Initial Version
*
*/
// - - - - - - - - - - - - - -
package org.clickblocks.dataStructure;
// - - - - - - - - - - - - - -
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
/**
* The class encapsulates a <code>ReferenceQueue</code>. It ensures
* that only kind of <code>Reference</code> objects that will be
* enqueued to the <code>ReferenceQueue</code> are
* <code>SoftCleanupReference</code> objects. It also encapsulates
* logic to tell <code>CleanupIF</code> objects enqueued to the
* <code>ReferenceQueue</code> to remove themselves from whatever data
* structure they are part of.
*
* @see CleanupIF
* @author Mark Grand
* @version 0.820
*/
public class CleanupQueue {
/**
* The <code>ReferenceQueue</code> encapsulated by this object.
*/
private ReferenceQueue refQueue = new ReferenceQueue();
/**
* This is true while a call to cleanup is pending.
*/
private boolean cleaning;
/**
* Return a soft reference that is registered to be enqueued to the
* queue that is encapsulated by this object.
*
* @param obj
* The object that the reference will refer to.
* @param cleanup
* The <code>CleanupIF</code> object whose
* <code>extricate</code> method is to be called after the
* soft reference is enqueued.
*/
public SoftReference createSoftReference(Object obj,
CleanupIF cleanup) {
return new SoftCleanupReference(obj, refQueue, cleanup);
} // createReference(Object, CleanupIF)
/**
* Call the <code>extricate</code> method of all enqueued
* <code>CleanupIF</code> objects. If there is currently a call in
* progress, then just return.
*/
public void cleanup() {
synchronized (this) {
if (cleaning) {
return;
} // if
cleaning = true;
} // synchronized
try {
while (refQueue.poll()!=null) {
SoftCleanupReference r;
r = (SoftCleanupReference)refQueue.remove();
r.extricate();
} // while
} catch (InterruptedException e) {
} finally {
cleaning = false;
} // try
} // cleanup()
} // class CleanupQueue