Friday, February 25, 2005

Why java.util.concurrent Kicks Ass and Takes Names

There are many things that JDK 1.5 [or Java 2 version 5 or Java 5 or Java 2 version 1.5 the entire marketing department should be the ones fired in the lastest round of layoffs from Sun because of their malicious and spiteful attempt to confuse the general public with f*cked up product names Edition] got horribly wrong, (generics, static imports, etc) but there are a few things that they got really right and I'm going to spend some time talking about some of my favorites and why it's important to my fellow bean slinger.

Besides fixing the broken memory model, which laid the ground work for some of the stuff we'll be taking about, the bumbling morons at Sun (I'm still bitter about WebServices, EJBs, and Corba to name a few) finally added something to Java I completely agree with and is worth its weight in Internet porn. This my friends is the java.util.concurrent package. Some of my new favorite things about Java are located in this package and its children. But before I get into the nitty gritty let me paint the big picture for you. So bear with me as we take a journey through my warped view of time and space and the events therein.

One of the coolest and more useful features of the Java language has been the simplicity with which Java allows its masters to write multithreaded applications. Until recently it's been damn hard to write kick ass multithreaded applications for everyday use for [at least] two reasons. This is not to say there are no kick ass multithreaded apps out there. Truth is, just about every Java application is multithreaded out of the box but [the bigger point I'm trying to make is] they are ham stringed by single processor deployments (reason #1). Which means, you can launch all the threads you want but if you have only one processor to execute them on you are not going to get too far over a single threaded application. As a matter of fact, you are probably going to make things worse. The other problem is, writing great performing multithreaded applications is just damn hard (reason #2). This problem isn't specific to Java but Java never made it any easier and in some ways made it worse. This is because of the lack of synchronization primitives that Java exposed/provided and the ones that it did (synchronized, volatile) had some serious problems, especially for the uninitiated. Finally, it's just plain old damn hard to write great performing, deadlock free, starvation free, race condition free, multithread applications (reason #2 prime). Unless of course, your application is grossly parallel by nature, which very few applications are. So what has changed and what is changing that has brought me out of my shell to evangelize Java 1.5? Let's start with what is changing.

Multi-core CPUs are coming to a desktop near you and it won't cost you both your arms, or your legs, or your first born. Think about how easy it is to buy a dual processor desktop/workstation today. Now imagine 6 months to a year from now buying that same dual processor workstation, but instead of 2 processors you really have 4. Are you excited yet!?! Do you see where I'm going with this!?! Huh? Huh? Huh? Imagine users plugging their ears because your Java application that used to crawl when running on a single processor system is now screaming like a banshee [with a plasma hot piece of metal shoved in places where the sun don't shine] on a 4 way processor system. Great! Now lets talk about what has changed.

Simultaneous multithreading processors are here now, actually they have been around a while (since 2002), it's just that now they are a lot more accessible. Intel's HyperThreading technology is the poster child for affordable simultaneous multithreading CPUs. IBM's Power series [especially Power 5] is pretty ass kicking and name talking too, it's just expensive as hell. With the Intel stuff you can go out and buy one right now without getting kicked out of your apartment cause you just spent the rent money or have your house foreclosed on unless, you are a college student, or just out of college, or still living with your mother, or recently divorced and she/he took everything, or just got fired and you need the money to buy a gun. What this all adds up to is the capability to write ass kicking, name taking, multithreaded applications today.

So now you are convinced, excited, and ready to cut code like a cook chopping onions for Carbonnade a la Flamande. As a matter of fact, there is a tear in your eye not from the onions but because you are so happy about the potential that lays before you. Well, I'm going to give you the opportunity to wipe your eye and blow your nose because I'm stopping here. We'll pick this up again later when I've got more free time, cause right now, to keep myself from being kicked out of my apartment and not get fired. I've got to get back to work.