Package java.lang.ref

Examples of java.lang.ref.ReferenceQueue


        if (initialCapacity == 0)
            initialCapacity = 1;
        this.$ASSIGN$loadFactor(loadFactor);
        $ASSIGN$threshold((int)(initialCapacity * loadFactor));
        theEntrySet = new WeakEntrySet();
        queue = new ReferenceQueue();
        setBuckets(new WeakBucket[initialCapacity]);
    }
View Full Code Here


    private final Set iteratorRefs;
    private final ReferenceQueue iteratorRefQueue;

    public WeakValueHashMap() {
  keyToRefMap = new HashMap();
  queue = new ReferenceQueue();
  iteratorRefs = new HashSet();
  iteratorRefQueue = new ReferenceQueue();
    }
View Full Code Here

        // and then further ensure that nothing else holds a strong reference
        // to the component (by waiting for the component to be garbage
        // collected).
        JComponent component = createTestComponent();
        JComponent overlay = createTestOverlay();
        final ReferenceQueue rq = new ReferenceQueue();
        final PhantomReference componentRef = new PhantomReference(component, rq);

        final JRootPane rootPane = new JRootPane() {
            public boolean isVisible() {
                return true;
            }
            public boolean isShowing() {
                return true;
            }
            protected JLayeredPane createLayeredPane() {
                return new JLayeredPane() {
                    public boolean isVisible() {
                        return true;
                    }
                    public boolean isShowing() {
                        return true;
                    }
                };
            }
        };
       
        final int lpcount = rootPane.getLayeredPane().getComponentCountInLayer(JLayeredPane.PALETTE_LAYER.intValue());
       
        OverlayHelper.attachOverlay(overlay, component, 0, 0, 0);
       
        assertEquals(lpcount, rootPane.getLayeredPane().getComponentCountInLayer(JLayeredPane.PALETTE_LAYER.intValue()));
        rootPane.getContentPane().add(component);
        // updateOverlay is "invokedLater", so wait for it...
        waitUntilEventQueueIsEmpty();
        assertEquals(lpcount + 1, rootPane.getLayeredPane().getComponentCountInLayer(JLayeredPane.PALETTE_LAYER.intValue()));
       
        rootPane.getContentPane().remove(component);
       
        // the remove operation make be invoked later, so wait for it...
        waitUntilEventQueueIsEmpty();
       
        // Clear out our strong references so it gets garbage collected.
        component = null;
        overlay = null;
       
        // Make sure the overlay was removed from the layered pane.
        assertEquals("It appears the overlay was not removed from the layered pane when its component was removed from the content pane", lpcount, rootPane.getLayeredPane().getComponentCountInLayer(JLayeredPane.PALETTE_LAYER.intValue()));
       
        // Make sure no other references are held... wait up to 15 seconds for
        // the object to be garbage collected.
        // Note: we may want to remove this section from the test as it
        // presumes the VM will garbage collect the reference fairly quickly
        // once a System.gc() is invoked.
        // There is no guarantee this will happen, so the assumption may be
        // faulty.  If it ends up being a problem, or starts yielding
        // inconsistent test results, then feel free to yank it.
        PhantomReference pr;
        final long end = System.currentTimeMillis() + 15000;
        do {
            System.gc();
        } while((pr = (PhantomReference)rq.remove(100)) == null && System.currentTimeMillis() < end);
        if(pr != null) {
            pr.clear();
        }
        assertSame("Either something else is still holding a strong reference to the component, or the VM is not garbage collecting it.  See comments in OverlayHelperTests.testRegressionOverlayHelperLeak() for more detail", pr, componentRef);
    }
View Full Code Here

        assertEquals(0, map.size());
    }
   
    public void testEntriesAreRemovedIfKeyIsGarbageCollected() throws InterruptedException {
        String key = new String("key");
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference ref = new PhantomReference(key, refQueue);
       
        Map map = new WeakCache();
        map.put(key, "value");
        key = null;

        int i = 0;
        while (refQueue.poll() == null) {
            ref.get(); // always null
            assertTrue("Key still alive even after "+i+" forced garbage collections", i++ < 5);
            Thread.sleep(10);
            System.gc();
        }
View Full Code Here

        assertEquals(0, map.size());
    }
   
    public void testSelfReferencingEntriesAreRemovedIfKeyIsGarbageCollected() throws InterruptedException {
        String key = new String("key");
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference ref = new PhantomReference(key, refQueue);
       
        Map map = new WeakCache();
        map.put(key, Collections.singleton(key));
        key = null;

        int i = 0;
        while (refQueue.poll() == null) {
            ref.get(); // always null
            assertTrue("Key still alive even after "+i+" forced garbage collections", i++ < 5);
            Thread.sleep(10);
            System.gc();
        }
View Full Code Here

        assertEquals(0, map.size());
    }
   
    public void testEntriesAreRemovedIfValueIsGarbageCollected() throws InterruptedException {
        String value = new String("value");
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference ref = new PhantomReference(value, refQueue);
       
        Map map = new WeakCache();
        map.put("key", value);
        value = null;

        int i = 0;
        while (refQueue.poll() == null) {
            ref.get(); // always null
            assertTrue("Value still alive even after "+i+" forced garbage collections", i++ < 5);
            Thread.sleep(10);
            System.gc();
        }
View Full Code Here

    }
   
    public void testSelfReferencingEntriesAreRemovedIfValueIsGarbageCollected() throws InterruptedException {
        String key = new String("key");
        Set value = Collections.singleton(key);
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference ref = new PhantomReference(value, refQueue);
       
        Map map = new WeakCache();
        map.put(key, value);
        value = null;

        int i = 0;
        while (refQueue.poll() == null) {
            ref.get(); // always null
            assertTrue("Value still alive even after "+i+" forced garbage collections", i++ < 5);
            Thread.sleep(10);
            System.gc();
        }
View Full Code Here

        File proxyToys = new File("target/lib/proxytoys-0.2.1.jar");
        ClassLoader classLoader = new URLClassLoader(new URL[]{proxyToys.toURI().toURL()}, getClass().getClassLoader());
        Class simpleReferenceType = classLoader.loadClass("com.thoughtworks.proxy.kit.SimpleReference");
        Field instance = simpleReferenceType.getDeclaredField("instance");
       
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference ref = new PhantomReference(instance, refQueue);
       
        Map map = new WeakCache();
        map.put(simpleReferenceType, instance);
        simpleReferenceType = null;
        instance = null;

        int i = 0;
        while (refQueue.poll() == null) {
            ref.get(); // always null
            //assertTrue("Value still alive even after "+i+" forced garbage collections", i++ < 5);
            if (i++ >= 10) {
                // actually never reached - unfortunately
                break;
View Full Code Here

        assertEquals(0, map.size());
    }
   
    public void testCanUseDifferentMapImplementation() throws InterruptedException {
        String value = new String("value");
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference ref = new PhantomReference(value, refQueue);
       
        Map map = new WeakCache(new TreeMap());
        map.put("key", value);
        value = null;

        int i = 0;
        while (refQueue.poll() == null) {
            ref.get(); // always null
            assertTrue("Value still alive even after "+i+" forced garbage collections", i++ < 5);
            Thread.sleep(10);
            System.gc();
        }
View Full Code Here

     */
    private void readObject(ObjectInputStream inp) throws IOException, ClassNotFoundException {
        inp.defaultReadObject();
        table = new Entry[inp.readInt()];
        threshold = (int)(table.length * loadFactor);
        queue = new ReferenceQueue();
        Object key = inp.readObject();
        while (key != null) {
            Object value = inp.readObject();
            put(key, value);
            key = inp.readObject();
View Full Code Here

TOP

Related Classes of java.lang.ref.ReferenceQueue

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.