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

    Object pool using phantom references

    a306c261-6fea-4c3f-834b-dd991134b962

      Hello,

      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);

            references.add(ref);

            return o;

          }

        }

       

        class PhantomThread implements Runnable {

          Thread thread;

         

          public PhantomThread() {

            thread = new Thread(this);

          }

         

          public void start() {

            thread.start();

          }

         

          public void run() {

            Field f;

            Reference<T> ref;

           

            try {

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

              while(true) {    

                ref = rq.remove();

                references.remove(ref);

                f.setAccessible(true);

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

                if(o != null) {

                  pool.add(object);

                }

                f.setAccessible(false);

              }

            } catch(Exception ex) {

              ex.printStackTrace();

            }       

          }

        }

      }

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

          Why?

           

          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.

          http://docs.oracle.com/javase/tutorial/reflect/member/fieldValues.html

          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.