December 2011
1 post
Thwarting Threading
It’s difficult to test concurrent behaviour, because there’s an arbitrary delay between calling start() on a thread and when it executes. That is unless you’ve discovered the CyclicBarrier. I’ve lost count of the number of bottlenecks and race conditions I’ve unearthed with this snippet. long timeMe(int threads) throws Exception { final CyclicBarrier start = ...
Dec 4th
October 2011
1 post
Concurrent Caching
I recently needed to write a caching layer with the following characteristics: If the key=value is missing, block on requests for this key until it is loaded. If the key=value is stale, allow one request to update it and return the stale value to further requests. This turns out to be extremely easy using the java.util.concurrent classes, specifically a little gem called an...
Oct 26th
July 2011
2 posts
4 tags
Profiling Production
I work a lot in Java and one thing it does well is profiling. IDEs like Netbeans have profiling options, that are also available as plugins to VisualVM which comes with the JDK. This is great for development, but I think its main use is to settle technical arguments. Things are a little different in production where hundreds of processes are running with all manner of dependencies. By far the...
Jul 15th
19 notes
5 tags
Mockito Magic
I like unit tests, for one simple reason. I don’t trust myself! Tell me your name and I’ll forget it. Give me something to look after, and I’ll lose it. Put something down for a minute and I’ll step on it.  Of course I’m exaggerating, I’m not that bad … or am I? Tell me your name, and I’ll probably use a mnemonic to remember it. Give me something to look after and I’ll include it in my...
Jul 12th
5 notes
April 2011
1 post
2 tags
Despicable Dates
A long time ago, I had the unpleasant task of writing a library for “intuitively” parsing Dates. As with most mental trauma, I cannot recall what happened, but I still fly into a blind rage when I see one written m/d/y. The other day I came across this neat piece of code for calculating a countdown broken down into days, hours, minutes and seconds: public void Countdown(long lo, long...
Apr 3rd
1 note
January 2011
3 posts
1 tag
Sequential SQL
Recently I wrote an interesting piece of sql using a cartesian product. The goal was to insert an item into the top-most position in a list given that some of the item positions are fixed. The table “list” is as follows: |item|rank|fixed| | a | 1 | 1 | | b | 2 | 0 | | c | 3 | 0 | | d | 4 | 0 | The idea is to insert the new item in the 0th position, and move every...
Jan 18th
1 note
2 tags
Troublesome Timestamps
A few years ago I was working for a company who would distribute a simple data file to it’s client app. The client app would scan the data file and construct an index of keywords together with the offset into the file for quick searching. As the data file grew, the time taken to index also grew and so it made sense to index the file and distribute both files together in a zip file. I wrote...
Jan 16th
Intro to tumblr
Perhaps I’m falling behind, as I only heard about tumblr in the last couple of weeks - referenced in some internet joke. I smiled, then immediately turned to google to find out what was so funny. Expect the next few posts to be a little random as I start to play, but I’m liking what I see so far.
Jan 14th