I’m sure most everyone has heard about the Sony laptop batteries that have been recalled due to a manufacturing defect that could cause them to catch fire. It started with Dell, spread to Apple and finally affected Toshiba.
My question is, why didn’t Sony recall any of it’s own laptop batteries? Do they buy them from someone else?
Ok, you’re wondering, what the hell is a Keyed Mutex. Understandable. Well, you see, it’s something I made up. Don’t get me wrong, I’m not claiming I invented anything – I’m sure something like this already exists, I just don’t know what it’s called or how to find it. So I made my own. Perhaps a little background would help.
I was anticipating a project that would require me to accept messages via socket and process them. The processing of each message could take up to a half a second so I wanted to provide a thread pool which would queue requests and process them concurrently. This would allow timely response to the socket client and multi-tasked processing of the messages. Note: this code would not be running in a JEE container where I could design something lovely like a JMS queue backed by a pool of Message Driven Beans.
One catch with my design was that each message represented a unique part number and it was imperative that two messages for the same part never be processed at the same time. What I needed was a reentrant mutex that was specific to each unique part number. This would allow the processing code to acquire a lock on the mutex for that part and cause any other messages for the same part to block until processing was completed.
I did some Internet searches but didn’t find anything that worked exactly like this. Oh sure, I could have used the ReeentrantLock class from Doug Lea’s util.concurrent library coupled with a HashMap but there were a couple of cons to this approach:
- It would have required including yet another third party library as this codebase was not yet under Java 5 where the concurrency classes are built in.
- I wanted the Map of mutexes to be dynamic. In other words be I wanted mutexes freed when not being used by any thread. Doug’s ReentrantLock class provides a way to determine how many locks the current thread has acquired but does not provide a good mechanism for determine how many threads are waiting on the lock. As a result I would never know if it was safe to discard the mutex object.
- I wanted to consolidate this functionality in a KeyedMutexManager that was easy to use and hide any complexity.
- I thought the world might someday need a mediocre Keyed Mutex class library. Ok, not really, but I do love to code and usually jump at any opportunity.
As things turned out the project I envisioned using this tool with was pushed out into the future but I thought I’d release the code in case it might help someone.
So, how does a Keyed Mutex work? Pretty simple really. When you have a thread that wishes to synchronize based on a unique String value you call KeyedMutexManager.lock(key). The KeyedMutexManager instance may be allocated globally and shared by all threads or instantiated by each thread. KeyedMutexManager works from a static HashMap so either approach provides the same result.
For example a thread uses KeyedMutexManager as follows:
public void run()
KeyedMutexManager mutexMgr = new KeyedMutexManager();
final Msg msg = getNextMsg();
Sorry for the long winded explanation. You can get the code here.
Recently while reading an English novel (i.e. a novel from the UK not one written in English) I encountered the word gobsmacked. I was dumbfounded so I looked it up on the web. Now I know that, not only was I dumbfounded, I was, in fact, gobsmacked!
Q] From W S McCollom: “I was looking at a UK magazine and ran across gobsmack. What can you tell me about this term?”
[A] It’s a fairly recent British slang term: the first recorded use is only in the eighties, though verbal use must surely go back further. The usual form is gobsmacked, though gobstruck is also found. It’s a combination of gob, mouth, and smacked. It means “utterly astonished, astounded”. It’s much stronger than just being surprised; it’s used for something that leaves you speechless, or otherwise stops you dead in your tracks. It suggests that something is as surprising as being suddenly hit in the face. It comes from northern dialect, most probably popularised through television programmes set in Liverpool, where it was common. It’s an obvious derivation of an existing term, since gob, originally from Scotland and the north of England, has been a dialect and slang term for the mouth for four hundred years (often in insulting phrases like “shut your gob!” to tell somebody to be quiet). It possibly goes back to the Scottish Gaelic word meaning a beak or a mouth, which has also bequeathed us the verb to gob, meaning to spit. Another form of the word is gab, from which we get gift of the gab.
If you didn’t know, now you do.
According to MSNBC:
INDUS, Minn. – A school principal has resigned and could face felony firearm charges after he shot and killed two orphaned kittens on school property last month.
Wade Pilloud, who resigned as principal of the K-12 Indus school, 40 miles west of International Falls, said he shot the kittens to spare them from starving to death after their mother was killed in an animal trap.
Let’s hope Condi doesn’t pick up on this as a possible solution to world hunger.
Loyal readers both know that I have, in the past, taken eReader.com to task for their high prices and reductions in their newsletter coupon code, Well a while back eReader.com restored their discount code back to 10% from the paltry 5% previously offered. This, no doubt, is a direct response to pressure generated by my blog post.
Now, dear readers, we need to continue the attack on eReader.com’s high prices!