Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I'm using the LWJGL libraries and unfortunately I need to free up texture/vbo buffers myself whenever a node in my scene graph needs to die, I can't even use finalize() method to do this as I can't guarantee that it will be executed in the same thread the opengl libs expects it to. So I'm using PhantomReferences. In my scene graph nodes I've put this in the constructor: phantomReference = new...
When someone is making bindings from a C library to Java (or any other garbage-collected language without destructors that are guaranteed to run), how do they deal with proper deallocation of non-garbage-collected memory? EDIT: What I'm thinking of (I know this isn't explicitly state in my original question) is when a piece of non-gc'ed memory holds references to other non-gc'ed resources that...
I'd like to wrap a C++ object so I can access it from Java. I have understood how to save a reference to my C++ object in my Java wrapper class by reading jni and using c++ new'ed objects in java. One thing I haven't figured out, though, is how to handle the creation and removal of my C++ object. Sure, I can introduce native methods that create and delete my C++ object but that means I have to ...
I have a TableHandler which implements table loading / reloading logic. By table I mean the data I load from either disc or memory when a user request for it in the form of table object. If a new request arrives and the instance of table is in memory then I just loads the table from memory and don't reloads it from disc. If the table I am looking for is not present in memory I just loads the ta...
I'm writing a DB connection pool in Java. It's just a class that holds a bunch of connections and gives them out. Do I need some kind of destructor method so that the DB connections will be closed when an instance of my class goes out of scope? Edit: This is for learning purposes only. I would definitely use a reliable, open source connection pool in production use. I really just want to u...
  /*
   * Copyright 1997-2005 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;

Reference queues, to which registered reference objects are appended by the garbage collector after the appropriate reachability changes are detected.

Author(s):
Mark Reinhold
Since:
1.2
 
 
 public class ReferenceQueue<T> {

    
Constructs a new reference-object queue.
 
     public ReferenceQueue() { }
 
     private static class Null extends ReferenceQueue {
         boolean enqueue(Reference r) {
             return false;
         }
     }
 
     static ReferenceQueue NULL = new Null();
     static ReferenceQueue ENQUEUED = new Null();
 
     static private class Lock { };
     private Lock lock = new Lock();
     private Reference<? extends T> head = null;
     private long queueLength = 0;
 
     boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */
         synchronized (r) {
             if (r.queue == return false;
             synchronized () {
                 r.queue = ;
                 r.next = ( == null) ? r : ;
                  = r;
                 ++;
                 if (r instanceof FinalReference) {
                     sun.misc.VM.addFinalRefCount(1);
                 }
                 .notifyAll();
                 return true;
             }
         }
     }
 
     private Reference<? extends T> reallyPoll() {       /* Must hold lock */
         if ( != null) {
             Reference<? extends T> r = ;
              = (r.next == r) ? null : r.next;
             r.queue = ;
             r.next = r;
             --;
             if (r instanceof FinalReference) {
                 sun.misc.VM.addFinalRefCount(-1);
             }
             return r;
         }
         return null;
     }

    
Polls this queue to see if a reference object is available. If one is available without further delay then it is removed from the queue and returned. Otherwise this method immediately returns null.

Returns:
A reference object, if one was immediately available, otherwise null
 
     public Reference<? extends T> poll() {
         synchronized () {
             return reallyPoll();
        }
    }

    
Removes the next reference object in this queue, blocking until either one becomes available or the given timeout period expires.

This method does not offer real-time guarantees: It schedules the timeout as if by invoking the java.lang.Object.wait(long) method.

Parameters:
timeout If positive, block for up to timeout milliseconds while waiting for a reference to be added to this queue. If zero, block indefinitely.
Returns:
A reference object, if one was available within the specified timeout period, otherwise null
Throws:
java.lang.IllegalArgumentException If the value of the timeout argument is negative
java.lang.InterruptedException If the timeout wait is interrupted
    public Reference<? extends T> remove(long timeout)
    {
        if (timeout < 0) {
            throw new IllegalArgumentException("Negative timeout value");
        }
        synchronized () {
            Reference<? extends T> r = reallyPoll();
            if (r != nullreturn r;
            for (;;) {
                .wait(timeout);
                r = reallyPoll();
                if (r != nullreturn r;
                if (timeout != 0) return null;
            }
        }
    }

    
Removes the next reference object in this queue, blocking until one becomes available.

Returns:
A reference object, blocking until one becomes available
Throws:
java.lang.InterruptedException If the wait is interrupted
    public Reference<? extends T> remove() throws InterruptedException {
        return remove(0);
    }
New to GrepCode? Check out our FAQ X