Comments

05/15/06: Trapped in the Closet

You've probably done this a thousand times:
private Map foos = new HashMap();

public void getFoo(String key) {
  synchronized (foos) {
    Foo foo = foos.get(key);
    if (foo == null) {
      foo = new Foo(key);
      foos.put(key, foo);
    }
    return foo;
}
If two threads are both trying to get the same item before it exists, the first one will create it, and the second one will block until it's ready. (I've heard of the dreaded double-checked locking conundrum, but I believe that if I close my eyes, plug my ears and say "nah nah nah" then it will never happen.)

This is a common enough creation pattern. So common, in fact, that it feels like there should be a type of Map that does it for you. Well, now there is :-) Christian and I call ours a Closet -- I suppose it's sort of a magic closet that creates things for you if they're not hanging in there already.

private Closet foos = new Closet() {
  protected Foo create(String key) {
    return new Foo(key);
  }
};

public Foo getFoo(String key) {
  return foos.get(key);
}

Much nicer, But wait, we're not done! There's a problem with this design in a world where create() takes a long time. (We came across it while trying to cache Lucene IndexReaders.) If two concurrent threads try to get *different* items, and the first one hasn't been created yet, then the second thread will block even though its item is ready and waiting.

We arrived at a fairly elegant solution to this problem, but the more interesting part was writing the tests first. Writing a unit test for a concurrency problem is always annoying. It's tempting to spawn a bunch of threads and then use sleep() either from your tests or from overridden methods, but that will make your tests slow, and even then they don't always work consistently.

It's better to model your tests either to *simulate* multiple threads, or to actually spawn threads but keep a tight reign on them. The subordinate test threads do a few things, then wait for the main test thread to tell them to proceed. That keeps the logic in your main test clean and simple, like a script for a play with several characters -- Joe does this, Bob says that, Joe walks there, Jane enters stage left.

Now all we need to do is add expiration logic and we've turned our Closet into a cache.

(Full code is unavailable for the moment, sadly... stay tuned.)

Comments made

When it's cold you want something that can really protect you from the cold and wet, you can opt for the <a href=http://www.cheapnorth.org>cheap north face</a> jacket. Because the North Face is such a great brand that its jackets can be quite costly. However,the cost at the North Face Outlet can save you a lot of money! When you are shopping for <a href=http://www.cheapnorth.org>cheap north face jacket</a> is definitely one of the best brands to stick with. A lot of the time there will be a surplus of the <a href=http://www.cheapnorth.org>north face</a> because other people don't want to pay that much for a jacket! This is why you can generally wait a bit longer and wait for the retailer to mark the price down before buying, because there will be plenty left to choose from. This is a good way to get cheap North face.
10/18/11 20:17:24
When it's cold you want something that can really protect you from the cold and wet, you can opt for the [url=http://www.cheapnorth.org]cheap north face[/url] jacket. Because the North Face is such a great brand that its jackets can be quite costly. However,the cost at the North Face Outlet can save you a lot of money! When you are shopping for [url=http://www.cheapnorth.org]cheap north face jackets[/url] is definitely one of the best brands to stick with. A lot of the time there will be a surplus of the [url=http://www.cheapnorth.org]north face jackets[/url] because other people don't want to pay that much for a jacket! This is why you can generally wait a bit longer and wait for the retailer to mark the price down before buying, because there will be plenty left to choose from. This is a good way to get cheap North face.
10/18/11 20:17:39
http://www.esnapbacks.org/

Although not every sport wears a snapback during a game, it is still part of the branding that goes along with every team.
02/27/12 17:36:32
The snapback hats http://www.tisasnapback.net are equally popular in all age groups no matter if you are girl, boy, child, youngster or a senior citizen.
02/28/12 23:56:20
http://www.ehats.org/
There are a lot of models and you can choose from a great range of colors. Most people prefer the snapback caps that have some tags on them or which have a different look than the ordinary ones.
03/01/12 23:19:48
This is perhaps the most identifiable feature on a pair of True Religion jeans. Impostor brands typically have a horse shoe that doesn't cover the entire area of the pocket leaving about a 1-inch "gap" between the bottom of the horse shoe stitching and the bottom of the pocket as well as using smaller "Levi's style" brass-colored rivets.

Oakley Women Sunglasses link: http://www.oakleyflakjacket...
Mens True Religion Bootcut Jeans link: http://www.cheaptroutlet.ne...
03/26/12 20:30:25

Add comment

Sorry, but due to blog comment spam, I have to ask you to create an account before you post a comment. Please log in (using the form on the top right of the page) or click here to create an account: Create an account!