Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Can someone explain the difference between the three Reference classes (or post a link to a nice explanation)? SoftReference > WeakReference > PhantomReference, but when would I use each one? Why is there a WeakHashMap but no SoftHashMap or PhantomHashMap? And if I use the following code... WeakReference<String> ref = new WeakReference<String>("Hello!"); if (ref != null) { ...
Is there a way to see all the references to an object in execution time? I'm using Netbeans, this feature exist in it? EDIT: No problem in using profilers to do this, I only need know the references, no matters how.
Working on a profiler of my own, I would like to explain what I see. There are some default threads which always appear, even in the simplest program: DestroyJavaVM Signal Dispatcher Finalizer Reference Handler Although their names are quite self-documenting, I would like to get a little bit more information. It seems these threads are not documented, does someone know a source to dig for t...
is there an elegant way in Java to set all existing object-pointers to null? I want an object to be deleted, but it is registered in several places an Event Listener.
Sorry for the stupid question. I'm very sure, that the Java API provides a class which wraps a reference, and provides a getter and a setter to it. class SomeWrapperClass<T> { private T obj; public T get(){ return obj; } public void set(T obj){ this.obj=obj; } } Am I right? Is there something like this in the Java API? Thank you. Yes, I could write it y myself, but why...
What is the common approach followed in applications if we have to differentiate between 'undefined' value and 'null' value for properties in a class? For example, let's say we have a class A public class A { Integer int1; Boolean bool1; Double doub1; String str1; } In the code, we would like to differentiate if each property of A is either set or not set (null is a VALID value to set)....
When I was studying about permgen, I came across the term hard reference. I don't know what is meant by hard reference. Can anyone explain or give me some tutorials about that please?
"There are only two hard things in Computer Science: cache invalidation and naming things" - Phil Karlton. I am writing a class which wraps an object with an expiry timestamp. The API looks like this: class ObjectWrapperWithExpiry<T> { private T obj; private long expiryTimestamp; ObjectWrapperWithExpiry(T obj, int ttl) { this.obj = obj; expiryTimestamp = System...
Ive been working on an app for a while that uses lots of Bitmaps and Ive gotten it to the point where its working really well on most devices that Ive tested it on except for a newer droid bionic that runs 2.3.4 I get an out of memory error and I can see the heap growing in the logcat too. So far Ive tried re-sizing the bitmaps with different techniques which works great for every other device...
SoftReference, WeakReference, PhantomReference may be used to customize the process of garbage collection. All of them extend Reference<T> therefore it is possible to mix them in single collection. Hard references (most common ones) do no extend Reference<T> therefore it is not possible to mix hard and other types of references in one collection. Am I right and we should put CustomR...
  /*
   * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.lang.ref;
 
Abstract base class for reference objects. This class defines the operations common to all reference objects. Because reference objects are implemented in close cooperation with the garbage collector, this class may not be subclassed directly.

Author(s):
Mark Reinhold
Since:
1.2
 
 
 public abstract class Reference<T> {
 
     /* A Reference instance is in one of four possible internal states:
      *
      *     Active: Subject to special treatment by the garbage collector.  Some
      *     time after the collector detects that the reachability of the
      *     referent has changed to the appropriate state, it changes the
      *     instance's state to either Pending or Inactive, depending upon
      *     whether or not the instance was registered with a queue when it was
      *     created.  In the former case it also adds the instance to the
      *     pending-Reference list.  Newly-created instances are Active.
      *
      *     Pending: An element of the pending-Reference list, waiting to be
      *     enqueued by the Reference-handler thread.  Unregistered instances
      *     are never in this state.
      *
      *     Enqueued: An element of the queue with which the instance was
      *     registered when it was created.  When an instance is removed from
      *     its ReferenceQueue, it is made Inactive.  Unregistered instances are
      *     never in this state.
      *
      *     Inactive: Nothing more to do.  Once an instance becomes Inactive its
      *     state will never change again.
      *
      * The state is encoded in the queue and next fields as follows:
      *
      *     Active: queue = ReferenceQueue with which instance is registered, or
      *     ReferenceQueue.NULL if it was not registered with a queue; next =
      *     null.
      *
      *     Pending: queue = ReferenceQueue with which instance is registered;
      *     next = Following instance in queue, or this if at end of list.
      *
      *     Enqueued: queue = ReferenceQueue.ENQUEUED; next = Following instance
      *     in queue, or this if at end of list.
      *
      *     Inactive: queue = ReferenceQueue.NULL; next = this.
      *
      * With this scheme the collector need only examine the next field in order
      * to determine whether a Reference instance requires special treatment: If
      * the next field is null then the instance is active; if it is non-null,
      * then the collector should treat the instance normally.
      *
      * To ensure that concurrent collector can discover active Reference
      * objects without interfering with application threads that may apply
      * the enqueue() method to those objects, collectors should link
      * discovered objects through the discovered field.
      */
 
     private T referent;         /* Treated specially by GC */
 
     ReferenceQueue<? super T> queue;
 
     Reference next;
     transient private Reference<T> discovered;  /* used by VM */
 
 
     /* Object used to synchronize with the garbage collector.  The collector
      * must acquire this lock at the beginning of each collection cycle.  It is
     * therefore critical that any code holding this lock complete as quickly
     * as possible, allocate no new objects, and avoid calling user code.
     */
    static private class Lock { };
    private static Lock lock = new Lock();
    /* List of References waiting to be enqueued.  The collector adds
     * References to this list, while the Reference-handler thread removes
     * them.  This list is protected by the above lock object.
     */
    private static Reference pending = null;
    /* High-priority thread to enqueue pending References
     */
    private static class ReferenceHandler extends Thread {
        ReferenceHandler(ThreadGroup gString name) {
            super(gname);
        }
        public void run() {
            for (;;) {
                Reference r;
                synchronized () {
                    if ( != null) {
                        r = ;
                        Reference rn = r.next;
                         = (rn == r) ? null : rn;
                        r.next = r;
                    } else {
                        try {
                            .wait();
                        } catch (InterruptedException x) { }
                        continue;
                    }
                }
                // Fast path for cleaners
                if (r instanceof Cleaner) {
                    ((Cleaner)r).clean();
                    continue;
                }
                ReferenceQueue q = r.queue;
                if (q != .q.enqueue(r);
            }
        }
    }
    static {
        ThreadGroup tg = Thread.currentThread().getThreadGroup();
        for (ThreadGroup tgn = tg;
             tgn != null;
             tg = tgntgn = tg.getParent());
        Thread handler = new ReferenceHandler(tg"Reference Handler");
        /* If there were a special system-only priority greater than
         * MAX_PRIORITY, it would be used here
         */
        handler.setPriority(.);
        handler.setDaemon(true);
        handler.start();
    }
    /* -- Referent accessor and setters -- */

    
Returns this reference object's referent. If this reference object has been cleared, either by the program or by the garbage collector, then this method returns null.

Returns:
The object to which this reference refers, or null if this reference object has been cleared
    public T get() {
        return this.;
    }

    
Clears this reference object. Invoking this method will not cause this object to be enqueued.

This method is invoked only by Java code; when the garbage collector clears references it does so directly, without invoking this method.

    public void clear() {
        this. = null;
    }
    /* -- Queue operations -- */

    
Tells whether or not this reference object has been enqueued, either by the program or by the garbage collector. If this reference object was not registered with a queue when it was created, then this method will always return false.

Returns:
true if and only if this reference object has been enqueued
    public boolean isEnqueued() {
        /* In terms of the internal states, this predicate actually tests
           whether the instance is either Pending or Enqueued */
        synchronized (this) {
            return (this. != .) && (this. != null);
        }
    }

    
Adds this reference object to the queue with which it is registered, if any.

This method is invoked only by Java code; when the garbage collector enqueues references it does so directly, without invoking this method.

Returns:
true if this reference object was successfully enqueued; false if it was already enqueued or if it was not registered with a queue when it was created
    public boolean enqueue() {
        return this..enqueue(this);
    }
    /* -- Constructors -- */
    Reference(T referent) {
        this(referentnull);
    }
    Reference(T referentReferenceQueue<? super T> queue) {
        this. = referent;
        this. = (queue == null) ? . : queue;
    }
New to GrepCode? Check out our FAQ X