The Artima Developer Community
Sponsored Link

Java Buzz Forum
Too Much Magic

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Brian McCallister

Posts: 1282
Nickname: frums
Registered: Sep, 2003

Brian McCallister is JustaProgrammer who thinks too much.
Too Much Magic Posted: Apr 20, 2004 3:57 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Brian McCallister.
Original Post: Too Much Magic
Feed Title: Waste of Time
Feed URL: http://kasparov.skife.org/blog/index.rss
Feed Description: A simple waste of time and weblog experiment
Latest Java Buzz Posts
Latest Java Buzz Posts by Brian McCallister
Latest Posts From Waste of Time

Advertisement

Leo Simmons charged me with using pooling from constructors. Well, why not. I don't advocate doing this at home though. Leo is a trained expert ;-)

We will be pooling that most vicious of resource-heavy classes, the HashSet. So, we build a basic HashSet pool (note we use a derivitive of HashSet so that I don't have to instrument the java runtime library).

package org.skife.pool;

import java.util.Stack;

public class HashPot {
    private Stack pool;
    public static final HashPot DEALER = new HashPot(5);

    public HashPot(int size) {
        pool = new Stack();
        for (int i = 0; i < size; ++i) {
            pool.push(new SpecialHashSet());
        }
    }

    public void release(SpecialHashSet set) {
        synchronized (set) {
            set.clear();
            pool.push(set);
        }
    }

    public SpecialHashSet obtain(){
        SpecialHashSet set = (SpecialHashSet) pool.pop();
        if (set == null) set = new SpecialHashSet();
        return set;
    }

    public int getFreeCount(){
        return pool.size();
    }
}

Nothin fancy there. The fun stuff comes in with the pooling...

package org.skife.pool;

public aspect HashPool
{
    pointcut create() : call(public SpecialHashSet.new())
                        && !within(HashPot);

    SpecialHashSet around() : create() {
        return HashPot.DEALER.obtain();
    }
}

Here we intercept the call to new SpecialHashSet() except within our pool (need to let the pool create them, after all).

Finally, we need to make sure it works:

package org.skife.pool;

import junit.framework.TestCase;

public class TestHash extends TestCase {

    public void testPullFromPool() {
        int start = HashPot.DEALER.getFreeCount();
        new SpecialHashSet();
        assertEquals(start - 1, HashPot.DEALER.getFreeCount());
    }

    public void returnToPool() {
        int start = HashPot.DEALER.getFreeCount();
        SpecialHashSet set = new SpecialHashSet();
        HashPot.DEALER.release(set);
        assertEquals(start, HashPot.DEALER.getFreeCount());
    }
}

And all these pass =) The SpecialHashSet is in fact pulled from the pool, rather than instantiated. Cool! Of course you still need to return the thing to the pool when you are finished with it. Java doesn't have a good way to detect "finished with it" but in Objective-C we could add a hook to the reference counter.

Read: Too Much Magic

Topic: Luxor XUL Meetup - Tuesday, May 4 @ 7:00PM - Vienna, Austria Previous Topic   Next Topic Topic: What's In A Name?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use