All entries for Thursday 19 January 2006

January 19, 2006

Living on the edge :)

So in the project I am working on we have a bit of code which checks the userAgent and decides whether it is "good" or "bad". It uses this information to determine the appropriate rendition, but that is irrelevant.

We have (as all good developers should ;)) and interface:

  public interface UserAgentChecker {
    boolean isGoodUserAgent(final String userAgent);

with a single implementation. Given that the number of unique user agents is quite small, but each user agent will be used a large number of times, (i.e. unique user agents would be Netscape, IE etc.) this is an ideal candidate for caching (high reads, low writes).

Also, given that the "goodness" or "badness" doesn't change, there is no need to worry about stale data. Even better.

Our existing implementation uses regular expressions which, considering their power are extremely efficient, but they are still (relatively) expensive.

To implement the cache I could use AOP, or I could use a simple decorator. Given that we are overriding all the methods (there is only one!) and the caching (at the moment) isn't reusable by lots of different things, I decided to go with the good old decorator pattern, i.e.:

  public final class CachedUserAgentChecker implements UserAgentChecker {
    public CachedUserAgentChecker(final UserAgentChecker decorated) {
      this.decoratedUserAgentChecker = decorated;

Because the size of the cache will be very small (tens of userAgent strings) I decided to go with a simple Map whose key is the userAgent and the value is Boolean.

Now, the million dollar question :)

Should that map be synchronised :) According to the Javadoc HashMap isn't synchronised so in theory if two threads execute a containsKey() or get() or put() it could throw a ConcurrentModificationException(), but the javadoc really isn't clear on whether it would.

I could do my own synchronisation, or I could use Collections.synchronisedMap(..) but this will all have a minor impact on performance.

So, should I or shouldn't I? I wrote a little test harness which started 100 threads which all did puts/gets/containsKey() and didn't get any concurrency issues.

So, have any of you ever run into concurrency issues using a standard HashMap?

At the moment the production code isn't synchronised ;) Hope I don't get sacked :)

January 2006

Mo Tu We Th Fr Sa Su
Dec |  Today  | Feb
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31               

Search this blog



Most recent comments

  • Interesting… While I'm not completely convinced in such microbenchmarks, I'm pretty sure that 1ms … by Alexander Snaps on this entry
  • Hello. I bought the book yesterday. I was trying to find the source code for chapter 11 and chapter … by Suleman on this entry
  • by live mashup demo on this entry
  • Thanks mate ….. This blog was really helpful. by Maaz Hurzuk on this entry
  • Ty. Not directly helpful for my problem, but pointed me in the right direction. You will also get th… by Mike E. on this entry

Blog archive

Not signed in
Sign in

Powered by BlogBuilder