Example usage for java.lang.ref SoftReference SoftReference

List of usage examples for java.lang.ref SoftReference SoftReference

Introduction

In this page you can find the example usage for java.lang.ref SoftReference SoftReference.

Prototype

public SoftReference(T referent, ReferenceQueue<? super T> q) 

Source Link

Document

Creates a new soft reference that refers to the given object and is registered with the given queue.

Usage

From source file:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }//from  w  w w.ja v  a2 s . c  o  m
    };
    Object data = new Object() {
        public String toString() {
            return "Data";
        }
    };

    HashMap map = new HashMap();
    Reference reference = new SoftReference(object, referenceQueue);

    map.put(reference, data);

    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    object = null;
    data = null;

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());
}

From source file:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }/* w  ww .j  a v  a  2s. c o  m*/
    };

    Object data = new Object() {
        public String toString() {
            return "Data";
        }
    };

    HashMap map = new HashMap();
    Reference reference = new SoftReference(object, referenceQueue);

    map.put(reference, data);

    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    object = null;
    data = null;

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());
}

From source file:SoftValueMap.java

protected Reference newReference(Object value) {
    return new SoftReference(value, refQueue);
}

From source file:org.entrystore.repository.impl.SoftCache.java

public void put(Entry entry) {
    synchronized (cache) {
        URI entryURI = entry.getEntryURI();
        cache.put(entryURI, new SoftReference(entry, clearedRefs));
        push(entry.getLocalMetadataURI(), entryURI);
        push(entry.getExternalMetadataURI(), entryURI);
        push(entry.getResourceURI(), entryURI);
        push(entry.getRelationURI(), entryURI);
    }/*from w  w w .jav  a 2  s.c  o  m*/
}

From source file:org.srs.vfs.VfsSoftCache.java

protected Reference<V> createReference(final V file, final ReferenceQueue<V> refqueue) {
    return new SoftReference<>(file, refqueue);
}

From source file:com.slytechs.utils.memory.Malloc.java

public void recordAllocatedBlock(Entry a, Buffer block) {

    /*//from  ww w .  j  a  v a 2  s.  c om
     * The software reference linked to reference queue "freedQueue". When
     * the referenced buffer is released, the reference is recorded in
     * the freeQueue so we can check and get a list of references (not the
     * buffer's anymore) that have been freed up by garbage collector.
     * 
     * Since the reference released is also the key in our allocated Map,
     * which stores the Entry value with the details of the allocated block 
     * (But no references to the buffer), we can later expand our free
     * blocks to reclaim the memory as its released.
     */
    SoftReference<? extends Buffer> pr = new SoftReference<Buffer>(block, freedQueue);

    allocated.put(pr, a);
}

From source file:org.orbeon.oxf.util.SoftReferenceObjectPool.java

/**
 * <p>Returns an instance to the pool after successful validation and passivation. The returning instance
 * is destroyed if any of the following are true:<ul>
 *   <li>the pool is closed</li>
 *   <li>{@link PoolableObjectFactory#validateObject(Object) validation} fails</li>
 *   <li>{@link PoolableObjectFactory#passivateObject(Object) passivation} throws an exception</li>
 * </ul>/* w ww  .  j  ava2 s  .  c om*/
 *</p>
 *
 * <p>Exceptions passivating or destroying instances are silently swallowed.  Exceptions validating
 * instances are propagated to the client.</p>
 *
 * @param obj instance to return to the pool
 */
@Override
public synchronized void returnObject(T obj) throws Exception {
    boolean success = !isClosed();
    if (_factory != null) {
        if (!_factory.validateObject(obj)) {
            success = false;
        } else {
            try {
                _factory.passivateObject(obj);
            } catch (Exception e) {
                success = false;
            }
        }
    }

    boolean shouldDestroy = !success;
    _numActive--;
    if (success) {
        _pool.add(new SoftReference<T>(obj, refQueue));
    }
    //        notifyAll(); // _numActive has changed

    if (shouldDestroy && _factory != null) {
        try {
            _factory.destroyObject(obj);
        } catch (Exception e) {
            // ignored
        }
    }
}

From source file:com.datos.vfs.cache.SoftRefFilesCache.java

protected Reference<FileObject> createReference(final FileObject file,
        final ReferenceQueue<FileObject> refqueue) {
    return new SoftReference<>(file, refqueue);
}

From source file:org.orbeon.oxf.util.SoftReferenceObjectPool.java

/**
 * <p>Create an object, and place it into the pool.
 * addObject() is useful for "pre-loading" a pool with idle objects.</p>
 *
 * <p>Before being added to the pool, the newly created instance is
 * {@link PoolableObjectFactory#validateObject(Object) validated} and
 * {@link PoolableObjectFactory#passivateObject(Object) passivated}.  If validation
 * fails, the new instance is {@link PoolableObjectFactory#destroyObject(Object) destroyed}.
 * Exceptions generated by the factory <code>makeObject</code> or <code>passivate</code> are
 * propagated to the caller. Exceptions destroying instances are silently swallowed.</p>
 *
 * @throws IllegalStateException if invoked on a {@link #close() closed} pool
 * @throws Exception when the {@link #getFactory() factory} has a problem creating or passivating an object.
 *///from w ww  .  j av  a  2 s  . co m
@Override
public synchronized void addObject() throws Exception {
    assertOpen();
    if (_factory == null) {
        throw new IllegalStateException("Cannot add objects without a factory.");
    }
    T obj = _factory.makeObject();

    boolean success = true;
    if (!_factory.validateObject(obj)) {
        success = false;
    } else {
        _factory.passivateObject(obj);
    }

    boolean shouldDestroy = !success;
    if (success) {
        _pool.add(new SoftReference<T>(obj, refQueue));
        //            notifyAll(); // _numActive has changed
    }

    if (shouldDestroy) {
        try {
            _factory.destroyObject(obj);
        } catch (Exception e) {
            // ignored
        }
    }
}