Search in sources :

Example 66 with Reference

use of java.lang.ref.Reference in project freemarker by apache.

the class TransformBlock method getSortedNamedArgs.

/**
 * Returns the named args by source-code order; it's not meant to be used during template execution, too slow for
 * that!
 */
private List getSortedNamedArgs() {
    Reference ref = sortedNamedArgsCache;
    if (ref != null) {
        List res = (List) ref.get();
        if (res != null)
            return res;
    }
    List res = MiscUtil.sortMapOfExpressions(namedArgs);
    sortedNamedArgsCache = new SoftReference(res);
    return res;
}
Also used : SoftReference(java.lang.ref.SoftReference) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) List(java.util.List)

Example 67 with Reference

use of java.lang.ref.Reference in project freemarker by apache.

the class ClassIntrospectorBuilder method build.

/**
 * Returns an instance that is possibly shared (singleton). Note that this comes with its own "shared lock",
 * since everyone who uses this object will have to lock with that common object.
 */
ClassIntrospector build() {
    if ((methodAppearanceFineTuner == null || methodAppearanceFineTuner instanceof SingletonCustomizer) && (methodSorter == null || methodSorter instanceof SingletonCustomizer)) {
        // Instance can be cached.
        ClassIntrospector instance;
        synchronized (INSTANCE_CACHE) {
            Reference instanceRef = (Reference) INSTANCE_CACHE.get(this);
            instance = instanceRef != null ? (ClassIntrospector) instanceRef.get() : null;
            if (instance == null) {
                // prevent any aliasing issues
                ClassIntrospectorBuilder thisClone = (ClassIntrospectorBuilder) clone();
                instance = new ClassIntrospector(thisClone, new Object(), true, true);
                INSTANCE_CACHE.put(thisClone, new WeakReference(instance, INSTANCE_CACHE_REF_QUEUE));
            }
        }
        removeClearedReferencesFromInstanceCache();
        return instance;
    } else {
        // BeansWrapper.
        return new ClassIntrospector(this, new Object(), true, false);
    }
}
Also used : Reference(java.lang.ref.Reference) WeakReference(java.lang.ref.WeakReference) WeakReference(java.lang.ref.WeakReference)

Example 68 with Reference

use of java.lang.ref.Reference in project ovirt-engine by oVirt.

the class ReapedMapTest method setUpGCExpectations.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void setUpGCExpectations(final int gcAfter) {
    ReferenceQueue<Integer> queue = mock(ReferenceQueue.class);
    map = new ReapedMap<>(10000, false, queue);
    final IdAwareReference ref = mock(IdAwareReference.class);
    when(ref.getKey()).thenReturn("three").thenReturn(null);
    // the gcAfter queue poll simulates a GC event and triggers deletion
    // on the reapable map
    when(queue.poll()).thenAnswer(new Answer<Reference<Integer>>() {

        private int times = 0;

        @Override
        public Reference<Integer> answer(InvocationOnMock invocation) throws Throwable {
            return times++ == gcAfter ? ref : null;
        }
    });
}
Also used : Reference(java.lang.ref.Reference) IdAwareReference(org.ovirt.engine.core.utils.ReapedMap.IdAwareReference) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IdAwareReference(org.ovirt.engine.core.utils.ReapedMap.IdAwareReference)

Example 69 with Reference

use of java.lang.ref.Reference in project openj9 by eclipse.

the class Test_Reference method test_enqueue.

/**
 * @tests java.lang.ref.Reference#enqueue()
 */
@Test
public void test_enqueue() {
    ReferenceQueue rq = new ReferenceQueue();
    obj = new Object();
    Reference ref = new SoftReference(obj, rq);
    AssertJUnit.assertTrue("Enqueue failed.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
    if (isJava8 || disableClearBeforeEnqueue) {
        AssertJUnit.assertTrue("Not properly enqueued.", rq.poll().get() == obj);
    } else {
        AssertJUnit.assertTrue("Not properly enqueued.", rq.poll().get() == null);
    }
    // This fails.
    AssertJUnit.assertTrue("Should remain enqueued.", !ref.isEnqueued());
    AssertJUnit.assertTrue("Can not enqueue twice.", (!ref.enqueue()) && (rq.poll() == null));
    rq = new ReferenceQueue();
    obj = new Object();
    ref = new WeakReference(obj, rq);
    AssertJUnit.assertTrue("Enqueue failed2.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
    if (isJava8 || disableClearBeforeEnqueue) {
        AssertJUnit.assertTrue("Not properly enqueued2.", rq.poll().get() == obj);
    } else {
        AssertJUnit.assertTrue("Not properly enqueued2.", rq.poll().get() == null);
    }
    // This fails.
    AssertJUnit.assertTrue("Should remain enqueued2.", !ref.isEnqueued());
    AssertJUnit.assertTrue("Can not enqueue twice2.", (!ref.enqueue()) && (rq.poll() == null));
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) SoftReference(java.lang.ref.SoftReference) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference) WeakReference(java.lang.ref.WeakReference) Test(org.testng.annotations.Test)

Example 70 with Reference

use of java.lang.ref.Reference in project openj9 by eclipse.

the class Test_Reference method test_get.

/**
 * @tests java.lang.ref.Reference#get()
 */
@Test
public void test_get() {
    // SM.
    obj = new Object();
    Reference ref = new WeakReference(obj, new ReferenceQueue());
    AssertJUnit.assertTrue("Get succeeded.", ref.get() == obj);
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference) WeakReference(java.lang.ref.WeakReference) Test(org.testng.annotations.Test)

Aggregations

Reference (java.lang.ref.Reference)74 WeakReference (java.lang.ref.WeakReference)34 SoftReference (java.lang.ref.SoftReference)33 ReferenceQueue (java.lang.ref.ReferenceQueue)13 Field (java.lang.reflect.Field)13 Map (java.util.Map)13 HashMap (java.util.HashMap)11 PhantomReference (java.lang.ref.PhantomReference)10 ArrayList (java.util.ArrayList)7 List (java.util.List)5 Tracing (brave.Tracing)3 Platform (brave.internal.Platform)3 TraceContext (brave.propagation.TraceContext)3 BufferedImage (java.awt.image.BufferedImage)3 VolatileImage (java.awt.image.VolatileImage)3 IOException (java.io.IOException)3 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)3 ConcurrentModificationException (java.util.ConcurrentModificationException)3 Iterator (java.util.Iterator)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3