1.1 --- a/lookup/src/main/java/org/netbeans/modules/openide/util/ActiveQueue.java Wed Jan 27 17:46:23 2010 -0500
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,108 +0,0 @@
1.4 -package org.netbeans.modules.openide.util;
1.5 -
1.6 -import java.lang.ref.Reference;
1.7 -import java.lang.ref.ReferenceQueue;
1.8 -import java.util.logging.Level;
1.9 -import java.util.logging.Logger;
1.10 -
1.11 -/**
1.12 - * Implementation of the active reference queue.
1.13 - */
1.14 -public final class ActiveQueue extends ReferenceQueue<Object> implements Runnable {
1.15 -
1.16 - private static final Logger LOGGER = Logger.getLogger(ActiveQueue.class.getName().replace('$', '.'));
1.17 - private static ActiveQueue activeReferenceQueue;
1.18 -
1.19 - /** number of known outstanding references */
1.20 - private int count;
1.21 - private boolean deprecated;
1.22 -
1.23 - ActiveQueue(boolean deprecated) {
1.24 - super();
1.25 - this.deprecated = deprecated;
1.26 - }
1.27 -
1.28 - public static synchronized ReferenceQueue<Object> queue() {
1.29 - if (activeReferenceQueue == null) {
1.30 - activeReferenceQueue = new ActiveQueue(false);
1.31 - }
1.32 -
1.33 - activeReferenceQueue.ping();
1.34 -
1.35 - return activeReferenceQueue;
1.36 - }
1.37 -
1.38 - @Override
1.39 - public Reference<Object> poll() {
1.40 - throw new UnsupportedOperationException();
1.41 - }
1.42 -
1.43 - @Override
1.44 - public Reference<Object> remove(long timeout) throws IllegalArgumentException, InterruptedException {
1.45 - throw new InterruptedException();
1.46 - }
1.47 -
1.48 - @Override
1.49 - public Reference<Object> remove() throws InterruptedException {
1.50 - throw new InterruptedException();
1.51 - }
1.52 -
1.53 - public void run() {
1.54 - while (true) {
1.55 - try {
1.56 - Reference<?> ref = super.remove(0);
1.57 - LOGGER.finer("dequeued reference");
1.58 - if (!(ref instanceof Runnable)) {
1.59 - LOGGER.warning("A reference not implementing runnable has been added to the Utilities.activeReferenceQueue(): " + ref.getClass());
1.60 - continue;
1.61 - }
1.62 - if (deprecated) {
1.63 - LOGGER.warning("Utilities.ACTIVE_REFERENCE_QUEUE has been deprecated for " + ref.getClass() + " use Utilities.activeReferenceQueue");
1.64 - }
1.65 - // do the cleanup
1.66 - try {
1.67 - ((Runnable) ref).run();
1.68 - } catch (ThreadDeath td) {
1.69 - throw td;
1.70 - } catch (Throwable t) {
1.71 - // Should not happen.
1.72 - // If it happens, it is a bug in client code, notify!
1.73 - LOGGER.log(Level.WARNING, null, t);
1.74 - } finally {
1.75 - // to allow GC
1.76 - ref = null;
1.77 - }
1.78 - } catch (InterruptedException ex) {
1.79 - // Can happen during VM shutdown, it seems. Ignore.
1.80 - continue;
1.81 - }
1.82 - synchronized (this) {
1.83 - assert count > 0;
1.84 - count--;
1.85 - if (count == 0) {
1.86 - // We have processed all we have to process (for now at least).
1.87 - // Could be restarted later if ping() called again.
1.88 - // This could also happen in case someone called queue() once and tried
1.89 - // to use it for several references; in that case run() might never be called on
1.90 - // the later ones to be collected. Can't really protect against that situation.
1.91 - // See issue #86625 for details.
1.92 - LOGGER.fine("stopping thread");
1.93 - break;
1.94 - }
1.95 - }
1.96 - }
1.97 - }
1.98 -
1.99 - synchronized void ping() {
1.100 - if (count == 0) {
1.101 - Thread t = new Thread(this, "Active Reference Queue Daemon");
1.102 - t.setPriority(Thread.MIN_PRIORITY);
1.103 - t.setDaemon(true);
1.104 - t.start();
1.105 - LOGGER.fine("starting thread");
1.106 - } else {
1.107 - LOGGER.finer("enqueuing reference");
1.108 - }
1.109 - count++;
1.110 - }
1.111 -}