1 Reply Latest reply on Apr 21, 2014 1:39 AM by rp0428

    Object pool using phantom references



      I really need some help with my program. I have to implement an object pool that uses phantom references to collect objects abandoned by client threads. This is what I have. I'm really not sure about this implementation. Any help would be greatly appreciated.



      class ObjectPool<T extends CloneableObject<T>> {

        private Queue<T> pool;

        private List references = new ArrayList();

        private ReferenceQueue rq = new ReferenceQueue();

        private CloneableObject<T> prototype


        public ObjectPool(CloneableObject<T> prototype) {

          this.prototype = prototype;

          pool = new LinkedList<T>();

          new PhantomThread().start();



        public T borrowObject() {   

          T o;

          synchronized(this) {

            if (pool.isEmpty()) {

              o = prototype.clone();   


            else {

              o = pool.remove();


            PhantomReference ref = new PhantomReference(o, rq);


            return o;




        class PhantomThread implements Runnable {

          Thread thread;


          public PhantomThread() {

            thread = new Thread(this);



          public void start() {




          public void run() {

            Field f;

            Reference<T> ref;


            try {

              f = Reference.class.getDeclaredField("referent");

              while(true) {    

                ref = rq.remove();



                T o = (T) f.get(ref);

                if(o != null) {





            } catch(Exception ex) {






        • 1. Re: Object pool using phantom references
          I have to implement an object pool that uses phantom references to collect objects abandoned by client threads.



          What PROBLEM are you trying to solve?


          Why have you chosen that as the solution?


          Why are you using reflection to try to modify the access parameters of a field? I say 'try' because your code can't possibly work.


          Tell us what YOU think this line of code is doing (hint: it is NOT doing what you may think):

          f = Reference.class.getDeclaredField("referent");

          That code is reflecting the Reference class itself instead of an 'instance' of the Reference class.


          See 'Getting and Setting Field Values' in The Java Tutorials for an example of using reflection.


          Book book = new Book();

          . . .

          Class<?> c = book.getClass();


              Field chap = c.getDeclaredField("chapters");

              chap.setLong(book, 12);

          See how that code does NOT use the 'Book' class but uses 'book' which is an instance of the class. That provides access to the instance of the 'Book' class. Then the 'setLong' method is used to set a value in the 'instance'.


          The class has NO actual values except those declared as 'static'. To work with 'instance' variables you need to work with the instance.