Sunday, December 22, 2024
Riffs from an overheated mind

Tags


Blogroll


Categories



Java Concurrency For Dummies: Pt 1 – The basics

July 31st, 2009 by hashbrown

During my recent week at the JavaOne conference, I attended several sessions dealing with issues of concurrency in programming. What started as a cursory interest, turned into a strong desire to really understand these problems at a deeper level. But I’m still really a noob when it comes to this stuff, so I figure this is a great time to try to explain it to other beginners.

Ok, let’s start with the basics, what is concurrency and why should you care? Well according to Wikipedia concurrency is

a property of systems in which several computations are executing simultaneously, and potentially interacting with each other. The computations may be executing on multiple cores in the same chip, preemptively time-shared threads on the same processor, or executed on physically separated processors.

Unless you’re writing applications that only have a few simultaneous users, chances are you will use the threading features of java to try and take advantage of the performance benefits of asynchronous processing. Even if you are writing a simple MVC style web app, that servlet container is going to be multi-threaded and you could encounter concurrency issues in your servlet code. The problem is, when we’re coding our app, it’s only ever exercised in a single-user scenario. And you can’t really write unit tests to check the behavior of multi-threaded areas of your code. Therefore, concurrency issues tend to not be identified during testing. Usually they appear as strange, random bugs during periods of heavy load, and cannot be reliably reproduced. Awesome. I love those.

So how do we identify the areas of our application that are susceptible to concurrency issues, and how do we code for them? Those are some pretty tough questions, I’ve found. Maybe by the end of this series we’ll have some answers. In the simplest of terms, managing concurrency means managing the shared state in your application, more specifically, mutable shared state. That really means any instance or static class level fields that are not declared as final. (There are some nuances to the use of the final keyword, but let’s keep it easy for now).
To control concurrency issues, follow 3 rules:

  • Don’t share state across threads. This is done by encapsulating your state. In the simplest example, this means that class fields are private and access is limited to one or two methods. This will make it easier to create code that is thread-safe.
  • Make your state immutable. If the value of your fields cannot be changed, you are already thread-safe! Consider exposing read-only versions of your objects when possible.
  • Finally, synchronize access. Only allow a single thread to access your state at any given time. Most concurrency discussion centers around locking and synchronization.

What does it mean to synchronize access? Synchronization is about controlling access to state and is handled primarily through locks. Proper synchronization guarantees 2 things:

  • Mutual Exclusion – this guarantees that only one thread can hold a given lock, and therefore only one thread has access to the locked state(data).
  • Visibility – this guarantees that as a thread releases a lock, any changes made to shared variables, the mutable shared state, will be immediately seen by all threads, most importantly the next thread acquiring the lock. This ensures that no threads see stale data.

So, concurrent programming in a nutshell:

Limit the mutable state (data that can be changed) in your objects. When allowing others to access and change your objects state, control that state using proper synchronization.

So that’s the absolute basics on concurrency, but what about Java?
The fundamental power of java is its memory model and the threading facilities that are built into the language. Along with thread support are many mechanisms to correctly handle concurrent operations. And this will be the subject of the rest of this blog series. Stay tuned.

Posted in geek stuff, programming | Comments Off on Java Concurrency For Dummies: Pt 1 – The basics

Java Interview Puzzler

June 18th, 2009 by hashbrown

This is a puzzler we created at my work to use as an interview problem. I had a fun time taking it myself, and got it wrong the first time! It was designed to mimic some bad legacy code you might encounter when working on a large aging system, that has passed through many hands. Terrible coding practices, no javadocs or comments, etc. You are tasked to figure out what the heck it does.
First – does it compile? If not, how do you fix it?
Second – If it compiles, what is the output?

I’ll answer questions and post the solution in the comments.

package crazy;

public class Timing extends R {
    public static final Integer t = new Integer("4");


    public static void main(String[] args) {
        Timing t = new Timing(11);

        System.out.print(t.foo);
        bat(t);
    }

    void fob() { fod(); }

    public Timing(int value) {

        super();
        foo = foo % value;
        bar();
    }

    static {
        System.out.print("6");

    }

    private void bar() {
        new Baz().bar(5);
        tr = "2";
    }

    private int foo = 25;


    static void r() { System.out.println("1"); }
}

abstract class R {

    String tr = "9";

    static void bat(R r) {
        r.fob();
        System.out.print(r.tr);

    }

    private int negate(int bam) {
        return bam * -1;
    }


    public R() {
        System.out.print("7");
    }

    void fod() { System.out.print((int)Math.floor(.99)); }


    protected final int t() {
        return 5;
    }

    class Baz {

        void bar(int fob) {
            if (fob == 9)
                System.out.print(String.valueOf(fob++));

                System.out.print(String.valueOf(negate(fob++)));
            if (fob == 5)
                System.out.print(String.valueOf(fob+=2));

        }
    }

    static {
        System.out.print("8");
    }

    abstract void fob();

}

Posted in geek stuff, programming | Comments Off on Java Interview Puzzler

My Week At JavaOne

June 5th, 2009 by hashbrown

I just returned home from a week in San Francisco at the JavaOne conference, I mean literally like 2 hours ago. This is the first time I’ve been to any conference like this, and I had a great time! The scale of this thing is really impressive, I mean it’s huge. The planning and logistics that go into putting on a conference of this size just amazes me. It felt like they really went all out, all the small details were handled, and you could see the effort they put into making it a special experience.

I had originally registered for tech sessions Tuesday – Thursday, but Thursday night I just wasn’t ready for it to end, and ended up cramming 2 more sessions in on Friday morning before jumping in a cab for the airport. I literally was in a cab 15 minutes after my final session.
Overall, I attended 19 technical sessions over 3 1/2 days, plus the general session keynotes.

I attended sessions on the different dynamic scripting languages that run on the JVM, many on concurrency, and then some on how to use those dynamic languages to solve concurrency problems. I had sessions on Spring, and REST using JAX-RS, sessions on unit testing and some of the tools available. I had a session on defective java with the man behind FindBugs, and a session on Effective Java (2nd Edition)
with one of the absolute rock-stars of the java world, Joshua Bloch. My session on Spring 3.0 was given by THE man behind Spring, Rod Johnson. My sessions on Lift and Clojure were both given by the creators of the language, and the session on JAX-RS was given by the writers of the specification. When I went to hear about how the Google App Engine now supports java, it was directly from the 3 guys responsible for it. I mean, talk about getting it from the source! That google session led to a very late night with my co-worker as we sat in the bar playing with the new sdk until 1am! Read the rest of this entry »

Posted in geek stuff, programming | Comments Off on My Week At JavaOne

Installing Rails, MySQL on Mac Leopard

April 19th, 2009 by hashbrown

Update: Some comments were mysteriously deleted, sorry if yours is not appearing, thanks for commenting though! One person responded that they needed to install the XCode tools first. I think they were building or upgrading ruby (which I didn’t do). There are certainly no issues with doing this step first, I just didn’t need to until I installed mysql.

I just recently finished tackling this oft attempted-failed-blogged-fixed(but not for you) ritual, getting a working ROR sandbox with mysql.
Many others have blogged about this, trust me I spent many days reading them, and the approaches and results are all over the map.
Sorry to say, this post is not the be-all end-all solution that other blogs claim. It’s just what I did, posted mainly for my own documentation.

First, my requirements. My web host, Site5, currently has installed Ruby 1.8.7 and Rails 2.2, plus MySQL 5.1.x. So I wanted my dev environment to be close to what my deployment environment will be.
I prefer to understand as much about my dev environment as I can, I want to know how things work, so that I can control them. So magic out-of-the-box solutions generally aren’t favored.
However, I’m generally the person that does it the hard way, and also runs into all those problems that people post about on the web, it never just works. And I’ve got a lot of grokking ahead of me as it is, so taking the easiest approach migh not be bad this time.

After researching this, I boiled down the options to 3 popular approaches:

  1. Use the default mac installation of ruby, upgrade the rails gem and install mysql
  2. Use MacPorts, which provides a pre-packaged install
  3. Build an entirely new environment from scratch, as detailed in the oft-cited Hivelogic article.

Option 1 seemed to be the easiest, but Leopard comes with Ruby 1.8.6 installed, I will use 1.8.7 in production, probably not a deal breaker. Also there is a potential that a future OS update could break something in my existing sandbox.

Option 2 gives me the magic all-in-one solution, but I always worry that I’m putting myself in their box. Plus the community seems pretty split on macports in general.

Option 3 gives me total control, and the best chance for everything going to hell.

In the end I decided on option 1, use the existing mac install and upgrade rails. I figured I start there, the other 2 options are still open to me if I find its needed.

“Finally, just get to it”. Surprisingly, everything worked very well, here’s how I did it.

In order to upgrade to 2.x rails gem, the gems package manager must be updated.

$ sudo gem install rubygems-update
$ sudo update_rubygems

Now update the rails gem.

$ sudo gem update
$ sudo gem update --system
$ sudo gem install rails

Now you can check your versions.

$ ruby -v
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
$ rails -v
Rails 2.3.2

Ok, time install mysql. First thing is to make sure Xcode tools are installed.

If you have your Leopard installation DVD, load it now. If you don’t have an installation DVD, you can download the Xcode 3.0 tools at Apple’s Developer Web Site. (Note: Apple developer accounts are free.)

Open the Optional Installs folder, and then the Xcode Tools folder. Double click on the XcodeTools.mpkg installer and select a standard install. This will take a few minutes to run.

Download MySQL from their site. Make sure to get the 32-bit mac version.
This a native mac application and is installed like any mac app.

Once mysql has been installed, the mysql gem must be installed.

There seem to be 2 secrets to make it work. First you point the gem to the mysql client config folder, instead of the mysql home. Second, if on an intel machine, provide the env ARCHTYPE flag.

$sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-
config=/usr/local/mysql/bin/mysql_config 

ok, and that’s it. Now to make sure everything is wired correctly, I just created a super simple rails app, following this guide.

Posted in Mac, ruby/rails | 3 Comments »

I Didn’t Switch To Macs

April 18th, 2009 by hashbrown

The New Macbook Pro
But I did just buy my first one. After a lifetime spent mastering windows, I never had a desire for a mac, and the fact that all the cool kids were getting them, made one even less desirable.
But lately I’ve wanted to get into some iphone development, and a mac is required. Also, I’ve really needed to purchase a new laptop. I’ve been using my work laptop as a personal computer for years, but it always feels borrowed.
So, given that the new intel based macs do a great job of running windows as well, I swallowed my pride and bought a macbook pro.

Now, I’m not going to get all fanboy about this. They do have a great aesthetic and its a pretty tight user experience, but in reality, as much as Vista has been bashed, the UI experiences aren’t that far apart. This isn’t some mystical new experience in computing. But I’ll save final judgment until I’ve really used it for awhile.

The first thing I really like about it, is that the terminal is a bash shell, no crappy ms-dos prompt. Plus 100 for the mac. The other thing I really love is that it seems to run MUCH cooler than my Dell, which would be burning up my lap in about an hour (yes I use a laptop on my lap).
But for all the talk about how great the design and UI is, I find that I don’t like the fact that every application looks like iTunes. It just gives it this rigid cold feeling, where every single app has to comply with the “apple way”. No individuals in this world.
I’m having a hard time adjusting to the command key vs ctrl on windows. Also, Apple provides a Control key as well, but I don’t know what it does. So far I use it just to control-click the track pad to enable “right-click” type features. Also, the mac keyboard has the “fn” key where the ctrl key is on windows, so I’m constantly hitting that by mistake. And I don’t like the key spacing on cut/paste which is cmd-c/cmd-v. I’m just so used to doing with my pinky and index finger on windows, the cmd and c/v keys are too close together. So I just have to get used to that I guess.
But my biggest beef is the menu bar. I’m not used to looking to the top of the screen to see the application controls, I expect them to be at the top of the application’s window. So I find myself constantly clicking around and application, trying to find a menu I’m looking for, until I final remember, “oh yeah, it’s up there”.
I’m not bashing it though, just the initial perspective of new user, coming from an alternate system.
We’ll see how it goes.

BTW – This mac does not make me cool. I was cool long before it kids.

-HB

Posted in Mac | Comments Off on I Didn’t Switch To Macs

Its been a long time…

August 3rd, 2007 by hashbrown

Since, I blogged? Yeah yeah, shut up. Can you believe it, original content? Anyway that’s not it.
Since I rock n rolled? Well technically no. i was just cranking out some massive solos in my studio here. But for real, yeah, a long time.
Still that’s not it. Its been a long time since I loved a cd. Radiohead only gives me one every 3 years or so. That’s long time to go without something that just stays in the player. I remember long ago, there was a time when every new album was exciting, it had the potential to be a new favorite. And there will still plenty worth at least buying. Nowadays, I purchase 1 or 2 a year if I’m lucky. Music has really let me down. Maybe I got old and let it down. I doubt it, I find it when its good, when it stands above the music that came before it, when it demands my attention. I think its that second point that gets harder through the years. What sounds old and done to me is something fresh and fun for the next generation. Christ you’d think I was an old man. I’m a hip programmer/musician in my 30’s. (That description could possibly be subjective). But this isn’t another bitch-fest post about the state of music today. (If you’ve got a blog post about it, please leave a link in my comments)
No, this is about finding that gem that stays in my cd player, that gives me hope, that makes me happy about music. I know I’m late to the game here, but I finally found Wilco. Specifically “Sky Blue Sky“.
Don’t know how I ended up with Wilco, all the way from Va Halen, Ratt and the Crue way back then, but here I am and here they are. And thank goodness, my soul’s been as dry as this Minnesota summer.

That’s it, just an exhale. Gotta find anyway back you can.

Posted in music | Comments Off on Its been a long time…

The latest “shiny social object”: an open/controllable social network?

August 2nd, 2007 by Robert Scoble

Poster inside a Facebook office

Well, I’ve been taking a lot of shots in the past few weeks for always covering the latest shiny social object. You know, first it was Twitter. Then Jaiku. Then Facebook. Pownce. On and on.

The critics say that either I’m late to the game, like with Facebook, or that I’m just too scattered and not looking for real value. Or that I don’t stay on one thing long enough to learn it well and add real value to my writings.

Fair enough. Although one thing I’d like to clear up. When I yammer on endlessly about Facebook that doesn’t mean I’ve stopped using any of the others. Twitter, for instance, is just a constant part of my life now.

Anyway, last night I was at the Facebook party aka “Lunch 2.0.” I met Mark Zuckerberg and his sister, Randi, and a ton of Facebook employees and executives. It is one of those parties that in about five years we’ll all be looking back on as a major inflection point in the valley. I stayed until the very end. In fact, even after the party ended a small group of us hang outside of Facebook’s offices and kept talking about what is going to happen in social networking.

One of the guys was John McCrea, vice president of marketing for Plaxo.

He told me that on Monday Plaxo i sgoing to turn on a new version. Ahh, a new “shiny social object.”

But then he explained why we should care: Plaxo is going to open up a new social network that’s both open as well as controllable. Translation: Plaxo is making a play for Facebook.

First, let’s go back to Facebook. Why does everyone say it’s a “walled garden?” Because you can’t get to data stored on Facebook unless you’re a Facebook member. Two days ago I did a video for Chris Pirillo on Facebook. Chris instantly got excited and wanted to share that with his blog’s readers. But he couldn’t. That video is locked inside Facebook’s walled garden. If you don’t have a key (a Facebook account) you can’t see it.

John told me that on Monday Plaxo will come out with a social network that gets rid of the walled garden.

Why did I say a couple of weeks ago that Facebook is a “data roach motel?” Because I can put all sorts of information about myself into Facebook (I could, for instance, tell you that I like “Daft Punk.” But, do I own that data? Can I get it out of Facebook? No).

John told me that on Monday Plaxo will come out with a social network that lets me own my own data and take it out of Plaxo and put it on other social networks.

Finally, I’ve been getting a few complaints about what I’ve been doing with Facebook. By turning Facebook into a professional networking tool I’m causing problems for people who saw it as a social tool to keep in touch with their college friends. See, Facebook for the first three years of its existence was mostly a tool for college kids to pass photos and other funny things around. Now, if you have photos of your frat party at college do you really want your new boss and coworkers to see those? Probably not.

But Facebook isn’t controllable. You can’t really have two groups of friends. One group that sees your drunken college frat photos and another group that sees you making presentations to your board of directors.

John told me that on Monday Plaxo will come out with a social network that lets me control which groups of friends (or family) that can see certain items.

OK, sounds like Plaxo is going to kill Facebook and bring down Facebook’s value by a few billion dollars. The bubble 2.0 will end. Zuckerberg will drag his tail away from the valley defeated. Etc etc etc. Right?

It’s not going to happen. Here’s why. It’s too late and the walled garden will keep people locked in.

Huh?

Sorry, Facebook already has momentum and a coolness about it that Plaxo doesn’t exude. I don’t really know how to explain the coolness without sounding really idiotic and goofy. That’s part of the 20-something vibe that Facebook has going for it right now. Oh, here’s a photo of me looking at the artwork in the Facebook offices. That might explain a little bit about it. There’s lots of other photos from the event last night here.

But it’s there and can’t be ignored. If John could explain to me how he’s going to get the world’s college students to look away from Facebook and toward something else maybe I’d go along with this “more open and controllable” Plaxo. My head is telling me that Plaxo is the way to go but my emotion tells me that Facebook is more fun.

The other thing is that BECAUSE of Facebook’s “closed” nature I’m not likely to leave it anytime soon. Why? Because if Facebook has 10% more content than the other networks do (which it will have because of the momentum that Facebook has today) that the more “open” networks will always seem lame in comparison.

But, on Monday I’ll try out the new Plaxo. I’m into “shiny social objects” and will report to you the pros and the cons. The problem, though, is that even if I get really excited about it my email is demonstrating that many of the world won’t be listening and won’t care.

What do you think?

After I get up this morning I’ll film a video explaining more of my thoughts and I’ll put that on my Kyte channel. For now Nokia’s CTO left me a little message there. I’ll do more stuff from the Always On conference today.

Posted in Facebook | Comments Off on The latest “shiny social object”: an open/controllable social network?

Videoblogger jailed

August 6th, 2006 by hashbrown

A guy I know, Josh Wolf, was just thrown in jail for refusing to hand over some videotapes he made of a July 2005 demonstration in San Francisco. The San Francisco Chronicle has the details.

The government has scary powers and is using them. Interesting to watch this case evolve and see how Josh is using his blog to get word out to his friends. Josh, on his blog, writes that he is planning to appeal this case all the way. That’ll cost $10,000 to $15,000 in legal fees. Whew.

This is scary. Head on over and do what you can.


Originally
from Scobleizer – Tech Geek Blogger

by Robert Scoble


reBlogged

by Hashbrown

on Aug 1, 2006, 11:24PM

Originally by Robert Scoble from Scobleizer – Tech Geek Blogger on August 1, 2006, 7:24pm

Posted in Uncategorized | Comments Off on Videoblogger jailed

Reblog 2.0 Beta 1 released

August 5th, 2006 by hashbrown

A fresh update to Reblog 2.0 has been released. Be first on your block to install it from http://reblog.org/#download.

This release includes a raft of enhancements, including better documentation for plug-in developers, slightly modified tag behavior that makes it easier to navigate your extensiv feed collection, experimental plug-ins for automatically publishing entries to WordPress, TypePad, Blogger and Del.icio.us accounts, and minor usability improvements too numerous to mention.

This BETA version has been extensively tested, and is recommended for most users. (1 comments)

check this out, its been released!


Originally
from SourceForge.net: SF.net Project News: reblog (including full news text)

by migurski@users.sourceforge.net (Michal Migurski)


reBlogged

by Hashbrown

on Feb 8, 2006, 6:20PM

Originally by migurski@users.sourceforge.net (Michal Migurski) from SourceForge.net: SF.net Project News: reblog (including full news text) on February 8, 2006, 1:20pm

Posted in Uncategorized | Comments Off on Reblog 2.0 Beta 1 released

Whoa, did you just post?

August 5th, 2006 by hashbrown

Hell yeah I did. So where have I been? Don’t you worry about it, I’ve been where most of us have been.

So this Reblog post you just saw. Don’t worry about that either, it was just a test. But if you’re curious I’m sure you can figure out part of what I’m up to. But I’ve got something more up my sleeve. I’ll be testing on this blog so you should start to see some results, some clues.

-HB

Posted in Uncategorized | Comments Off on Whoa, did you just post?

« Previous Entries