Monday, June 22, 2009

P90X Week One










Ok, so I started P90X 4 days ago and it's kicking my arse. This is week one but, the work outs are really well structured day one, pull ups and push ups, followed by plyometrics (this left me sore for 2 days), next they bring arms and shoulders, and then the hardest (longest) Yoga work out I've ever done. Forget it these guys are strong, fit, and flexible and I'm gonna keep up with the program. The goal is to use this program and a light cardio program as well (swimming, biking, and running). The program so far is perfect in that we are alternating muscle groups then a couple days after the plyo work out you jump into a long stretching Yoga work out. Well today I'm gonna swim for 1/2 a mile then do the work out (Legs and Back) at night (I like the night work outs cause it gives me something to do at night at home). Finally, I've changed my diet up but, I'm finding myself hugry AHHH.. we'll I'm keeping at it high protien low carbs so...

So here's the week one pics.

Thursday, March 19, 2009

Writing Methods? Less is More!!

This is a lesson I learned as a young aspiring programmer. I was in my early 20s and just about to complete the 200th line of code in a single method when it dawned on me, I sure hope nothing goes wrong with this method. We’ve all seen it and we’ve all done at some stage in our development life. The goal of my nice little blog is to bring visibility to the problem and give you some guidelines for writing methods, which ultimately will lead to reusable code, easier debugging and better code management.

Try to limit your methods to around 35 lines, what you really want, is to be able to see the entire method on the screen. Limit how far out you go horizontally to achieve this same goal. You want to be able to look at the entire method without scrolling. I will guarantee that if you need more that 35 lines of code in a single method you have code that can (maybe not now) be reused. Make methods to copy properties, use libraries that will copy your bean properties from a value object to another object (jakarta has a nice library to do this). Using this best practice will help you start thinking about code reuse and will help the next guy/gal that has to debug your misbehaving method get a good understanding of what’s going on faster.

Till next time,

Chris

Concurrency issues with Java

What I really want to discuss is a recent experience I had with a client and the first task I had on site with them.

client: “Hey we have some unsafe thread code, we need you guys to fix it.”
me: “Okay, but is there a specific example of what makes you feel there’s unsafe thread code?”
client: “Yeah, everyone that uses the system says, that every once in awhile they’ll try to go view a particular piece of content, and they get a completely different unrelated piece of content.”

Ahhhhhh, sounds like a concurrency issues! Here’s a little history about this application and the singleton design pattern and how it was misused, and a couple solutions. This particular application was written with the Spring Framework and a very common thing to do [okay, almost always] is create singletons. Huh, what’s a singleton? A singleton is a common design pattern. It refers to a class which is not meant to be freely instantiated, but have only one instance. If you’ve ever used Servlets then you know all you need to know, unless you don’t understand the stateless programming paradigm, ahhhhh.

Let’s talk about the definition I gave you above for the singleton pattern and discuss stateless programming for a minute. The singleton pattern says you only have one instance. This simply means that for every user/thread accessing the singleton will get the same one that every other user/thread got. Okay, got it, cool let’s talk about how this can get you in trouble.

1. Question, what happens if you have a singleton class with a class level attribute and a user/thread alters the state of that attribute via a public method? Answer, the attribute is changed for every user/thread that is accessing the singleton instance.

2. Question, does the JVM wait for the completion of the current executing method call before executing the same method call from another thread? Answer, well okay it depends, we can use the synchronized keyword to force the JVM to do this, but I’ll answer it like this NO, it doesn’t wait, and depending on how the virtual machine queues the thread for execution doesn’t even ensure a thread that started before yours will go first.

3. Question, who knows what else we can call the condition created by question one and question two? Answer, a race condition, nice one!

Okay, but what does this mean. It means we create scenarios like this in systems that have concurrent access:

client: “Yeah, everyone that uses the system says, that every once in awhile they’ll try to go view a particular piece of content, and they get a completely different unrelated piece of content.”

Guess what 9 times out of 10 a developer guilty of writing race condition code would never see the problem. Why? They generally only access the system with a single thread of execution [ie. they’re the only ones using the system]. Often times this passes right by a QA team too, why, because they aren’t creating concurrency on the system either.

So how do we fix it? The thing is we need to put all our code in a method block or we need to synchronize the method call. I’ll recommend the… put all your code in the method block, the synchronized keyword on a method in Java means that we block until that method has completed, this means Java will queue all the threads wanting access to the method until it’s complete. Little side note for you; singleton class level attributes are fine if you want the change to be acknowledged by every user/thread accessing the singleton class, but just make sure that’s the case and it’s not dynamic data; the dynamic data needs to live inside a method block.

Simple Illustration:

Here’s what I’m trying to illustrate let’s say user 1 accesses the loadMyUrl(int id) method and pass an id of 2 in, notice we just reassigned the class url. If user 2 accesses the loadMyUrl(int id) method and pass an id of 3 in, the JVM will not guarantee that user 1 will get the url he expected same goes for user 2. If you do I’d call it the luck of the Irish and don’t bet on it.

class BadContentGetter{

string url;

public string loadMyUrl(int id){
url = getMyUrl(id);

return url;
}
}

So how do we fix it and how did I fix it in the real scenario mentioned above? It’ really quite simple, remove the class level attribute and put the attribute in the method. You are now stateless.

class GoodContentGetter{

public string loadMyUrl(int id){
string url = getMyUrl(id);

return url;
}
}

Here’s how I like to think about the singleton pattern, it will give me a new instance of the code executing inside a method and give me the same class instance always, don’t new it up please. If you really think about this pattern it’s pretty amazing. Think about this for a second if you use a stateful paradigm you have to have something that ensures that everyone/thread gets their own copy, which means some kind of newing up process or some kind of pool access and pool return over head. Using stateless programming and the singleton pattern you don’t need this, and in my opinion is solid as a ROCK ;)!

Cheers till next time.

Java Speech Recognition and Adobe Flex (Let's Talk!)

First things first, here’s the demo http://ibex.clearfly.net:8080/recognition/Recognition.swf now, on with the blog….

Java and Flex’s capabilities are the natural fit for the next generation of the web (I dare not say Web 3.0) where face recognition sign ons, video chatting, and speech enabled navigation reign king. Hmmmmm you might say, or maybe your thinking about a computer named HAL 9000 from the Space Odyssey saga (fun little fact for my readers, the letter after H is I the letter after A is B and the letter after L is M; IBM). Well I know my imagination was running wild when I finally put my speech activated navigation system together.

How it all began was about a month ago I was asked to see the viability of using a speech recognizer on the web, I was asked to give consideration to a system that could help people learn how to read online. With this I set off on a journey to see what was out there in the open source world to help me along in my endeavors.

The first stop on the road to finding all the pieces to my online speech recognition system was OBVIOUSLY a speech recognition piece of software. I ended up taking about a week to find the speech recognizer I wanted to use. During this week I researched a number of speech recognition items and researched speech recognition in general as well. Speech recognition is a very complex matter with lots of concepts and vocabulary that “speech experts” use when discussing the topic. I felt it was of critical matter to understand terms like Hidden Markov Models (HMM), Utterances, Speech Models, Trained Speech Models, Acoustic ranges, Terrace searching, and number of other related concepts. During this week I evaluated a number of speech recognition tools including Sphinx, Nuance, VoiceBox, and the Microsoft speech server. In reviewing these I was looking firstly at finding the “best of breed” speech recognizer the one(s) most commonly used. In doing this I found that Nuance had the most widely used speech recognizer, however it only had a .NET API and the product is commercial as well, I’m a Java guy and on a tight budget. In reviewing the other open source (targeting java of course) products I found that most of them simply incorporated “CMU Sphinx” as their speech recognizer. So this began the process of evaluating and understanding Sphinx. Sphinx is quite nice and I think you’d be very impressed with what it gives you right out of the box. So there I have it, the first piece of my online speech recognition system. Next stop, feed the recognition system from a web browsers hmmmmmm.

So now the question became, how do I get the users voices into the speech recognizer from the web. Clearly, I’m going to need a browser plugin, html and javascript aren’t going to give me microphone support. Ok, ok, but which technology to use? Applets? nahhhhh nobody has the plugin anymore, sorry sun it was a good try but Flash is King when it comes to browser support and user adoption in the rich internet application RIA space, after all what site doesn’t use Flash to show advertisements. So there I go, Flash it is, but to be honest I’m not that great when it comes to building things in Flash. Yeah, I’ve made some fun cartoons with the kids and made a couple banners but, this was a system an online application, I’m gonna need common html like features! Have no fear Chris, your surrounded by Flex developers :) one of the perks of my job. I really hadn’t played around with Flex before this project, I’d listened to a presentation on it by Leif Wells and I had done some reading on it, but the opportunity hadn’t risen to put it to use. Flex is what OpenLaszlo tried to be :) that little note is for you old guys like me. However, now I’m a Flex junkie, I love it! The only thing faster than developing in Flex is downloading an existing open source project and putting it on a server. Something I really appreciate from the Adobe guys is their HUGE embrace of the open source community and for establishing a project called BlazeDS. This is a Java integration point to Flex and if your a Spring Framework guy like me then I’m telling you right now, forget about Spring MVC, JSF, Seam, Struts, or any of your other frameworks and learn Flex with BlazeDS. It’s simply too easy to deploy scalable tiered applications. You only need to write your Service and DAO layers and expose them to Flex via BlazeDS and you now have a remoting client calling your service layer, that’s how it’s supposed to be right. I’m on my way now right! Flash has microphone support, now I just need to get up to the server have a service grab this speech and pipe it through Sphinx and see what the users are saying right?!?!

Hold the press!!!

BlazeDS doesn’t support the Real Time Messaging Protocol RTMP (it’s ok I didn’t know what it was either till I needed to use it). RTMP is a how Flash “publishes” streaming video and audio to the server and since we’re using BlazeDS as our remoting end point he was the guy I was looking to, to handle this. Here’s something to keep in mind, the RTMP protocol is now open source THANKS ADOBE! but, BlazeDS doesn’t support it yet! On a side note, it used to be you had to buy the Flash Media Server to interact with video and audio media types but, not anymore there’s a very well known open source media server called Red5. Thank goodness, cause the Flash Media Server has a nice price tag (nice if your Adobe that is). So, now I’ve really got it, I have the plan of attack and goes like this.

Step 1: Publish “utterances” to the Red5 server.
Step 2: Tell our Spring service layer to process this (right now we take the *.flv that’s published and pipe it through ffmpeg to output .wav for Sphinx).
Step 3: Send the response back to the client.
Step 4: Let the client take actions based on what it understood (ie. open a tab or something)

Got it?
Easy enough!
Wanna see it?
Here you go: http://ibex.clearfly.net:8080/recognition/Recognition.swf (once your on the site, there’s some videos to tell you how to use it)

Enjoy the demo and let me know what you think!