Writing better JNI code

Well, it seems Ron beat me to the punch to blog about this – but allow me to promote the recently published developerWorks article that I had a hand in bringing to life:  Best practices for using the Java Native Interface

Here is the summary:

The Java™ Native Interface (JNI) is a standard Java API that enables Java code to integrate with code written in other programming languages. JNI can be a key element in your toolkit if you want to leverage existing code assets — for example, in a service-oriented architecture (SOA) or a cloud-based system. But when used without due care, JNI can quickly lead to poorly performing and unstable applications. This article identifies the top 10 JNI programming pitfalls, provides best practices for avoiding them, and introduces the tools available for implementing these practices.

Many kudos to Michael Dawson for his wordsmithing and persistence to make this article happen, he really deserves the lion’s share of the credit.

Ron calls out his favorite pitfall as using the wrong JNIEnv.  I’ll pick the one that makes me laugh because it is sad but true:  Choosing the wrong boundary between native and Java code – believe it or not there was a real world example that motivated the sample code.  The customer had decided to go for a “pure java” solution for their embedded application, so the minimal amount of native code was written – two functions that could read or write individual bits (not bytes).  While the example is outlandish, many times the integration of legacy code via JNI is done without thinking of the cost of the boundary.

Let’s discuss the picking the right side of the JNI boundary for a minute.  The NIO implementation is a good example of trying to fix file performance by keeping the data on the “right” side of the JNI boundary.  While NIO provides other functions allowing for tighter OS integration, one key performance win is due to the file data is kept in native buffers.   If you’re trying to get the data into java objects to be manipulated, you are likely defeating the performance win of NIO by forcing all that file data across the JNI boundary.  Where NIO will excel is needing to pull a bunch of data from a file and put it in another file – if you don’t need to hoist that data up into the java object space, you’ll get close to pure native performance.

Please check out the article, or at least bookmark it and send it to people who write JNI code.   Maybe we’ll see fewer false JVM bugs that turn out to be JNI related problems.

Managing Cable Clutter

This is something I actually did back in February when we moved our computer room to make room for our new arrival.  This is one of those DIY tasks that doesn’t take very long to do (a couple of hours tops), but you can put it off for ages (sort of like writing this post).  Anyone dealing with a rats nest of cables behind / below their desk, maybe this will serve as a kick in the pants to do something about it.

The LifeHacker article inspired me, but clearly not enough to quickly leap on the idea and deal with my mess of cables.  They actually have a whole category dedicated to cable management which is worth a browse if you are looking for ideas.  The original concept came from a site which seems to be gone now, but can still be seen via the Way Back Machine.

img_0670So pictured above is my version of the peg board cable organizer.  I’ve got a 2′ x 4′ peg board mounted on the back of the desk (between the desk and the wall).  There are only 2 screws holding the board to the desk (upper left and right corners).  I’ve used a couple of washers between the peg board and the desk to give it a bit of room for the cable ties.  Everything is zip-tied to the board, power bar included.  Since I’ve only got 2 screws at the top, I can bend the board (gently) away from the desk to get easy access to the back side to help route the zip ties.

img_0673The view from the front of the desk is what makes this worth doing.  No clutter on the floor (ok, an errant ethernet cable and the USB to the printer).  Even for my desk (pictured is Jenn’s) – where I need to plug and unplug things like my laptop regularly, this setup works well.  I simply mounted the powerbar near the top of the peg board and that places the outlets near enough to the top of the back edge of the desk that its not a problem.

This project only took a couple of hours, it is very functional – and tidy too.  What are you waiting for?

iTouch

The gadget lust was finally too much and I’ve purchased an iPod Touch.  Way back I was a fairly early adopter of the Palm Pilot 1000, however its been years since I’ve bothered to carry a PDA with me.  Cell phones have mostly closed that gap and the internet is more ubiquitous than it was back in 1996, these two technologies which have radically changed the information landscape around us.  While I do tease some of my friends with the iPhone (I charge my Nokia 5310 once a week), I’d have an iPhone if I could justify the cost of the monthly plans.

I’m a big fan of the Apple store online, specifically the refurbished section.  They offer “as new” products for a significant discount.  The refurbished iPod Touch has a new battery and outer shell, new earbuds, USB cable and it comes with the same 1-year warranty that a new unit would.  The only real difference is price (8Gb iTouch $199 vs. $239) and a plain white box.  If you can stand to wait until the latest model starts to be available refurbished, why pay full price?

I ordered monday, it arrived thursday.  Shipping was free.  Surprisingly it came from Shenzen, home of Foxconn (I found Bunnie’s description of Shenzen facinating, worth a read).  To give you a sense of the scale of the voyage that this lowly iPod Touch made over the past week – here is a map showing the voyage:
fedex_ipod

As I already have a video iPod, getting setup was very simple.  Using the latest version of iTunes, I just plugged in the new iTouch to let it charge and sync.  The initial sync was very slow, apparently this is somewhat expected.

As to exactly why I went out and bought an iTouch?  Well, it was a Fathers Day gift (Thanks Jenn!) – but I’ve been oogling at them for a while.  This LifeHacker article could serve as a good set of reasons to pick one up instead of holding out for an iPhone.  The 2nd generation iTouch is more compelling than the 1st – it has a faster processor (faster than the iPhone 3G, but not the 3G S), the headphone jack supports microphone input, there is also an external speaker.  In comparison to the iPhone – the 2nd generation  iTouch is missing: a camera, GPS, and of course cell phone functionality.  It does have almost every other ability.

So far its been very cool.  With only 8Gb of storage, I can’t get all my music on it – but iTunes seems to have done a fairly sane job of putting the right selection on without any interaction from me.  The App Store is really cool (but there are far too many apps out there).  If you need more convincing to jump on the Apple train, Jeff Atwood wrote an interesting commentary on the significance of the iPhone.