Friday, March 16, 2012

What is AtomicReference for really?

we had a discussion about AtomicReference at work the other day, it appears that none of us really understand why we should use AtomicReference based on a quick glimpse at the java doc provided.

The confusion comes from that we all firmly believe reference assignment is atomic in java, so why both with AtomicReference.

I decided to do some research on it and found this great post: Memory Barriers. It looks like there are several benefits of using AtomicReference.

Internally AtomicReference marks the value volatile, which creates a memory barrier that a, makes sure no thread is reading out dated data, b, provides happens-before guarantee because your code won't be reordered by compiler. If you only use the get/set method of AtomicReference, you are essentially marking your variable volatile in a fancy way.

The other interesting method is compareAndSet(expected, newVal). With care, AtomicReference can be used as an efficient way to do synchronization, especially when concurrent change happens rarely. to do this, the thread calls compareAndSet instead of set. If it fails, then it means that the value had been updated, so this thread has to deal with the conflict, such as abandon its change, overwrite, or merge.

In summary, AtomicReference differs from other atomic types in the concurrency package that it doesn't offer the typically desired atomic action like getAndIncrement, but they are similar that they all provide compareAndSet methods.

Tuesday, April 26, 2011

content assist issue in Flash Builder eclipse plugin

I had some rough time trying to get standalone flash builder version 4 to work with Java code and Tomcat. After hours of scratching my head, I gave up. It just shouldn't be this hard. But if you are adventurous enough to give it a try, here is a great tutorial you may want to read first:

So my next logical step is to install flash builder plugin to an eclipse installation. I know eclipse pretty well so the java/maven/wtp/tomcat parts went smooth without any drama. Flash builder plugin installed fine. And I was able to import flex projects and tweak the class path to make it build. The only thing missing in this setup is content assist, where I don't get code complete, API suggestions, or open declaration (F3) etc.

After two days of suffering without content assist and numerous googling, I finally found this great discussion which gave me a pointer to get it fixed. :)

The culprit: Flash Builder doesn't work well with m2eclipse. Importing flex project as maven project confused the crap out of flash builder.
So what I end up doing is to start with clean project checkout, then in eclipse, create a new flex project with the same name as the checked out project, uncheck the "default project location" and point it to where the project is checked out. then tweak the src folder and build path to make the new project look just like "we imported it from the SCM checkout". After that content assist works like a charm.

Monday, March 14, 2011

NavigableMap in JDK6

JDK6 introduced NavigableMap and retro-fitted TreeMap to implement this interface. I was a bit confused at first about ceilingKey vs higherKey and floorKey vs lowerKey. It turns out that floor and ceiling operations can return equal entries, while higher and lower operations must be strictly less than or greater than.

Nice features. I can definitely find some uses for them. :)

Tuesday, November 23, 2010

Use compiler to catch new enum value not covered in "switch"

This old trick shines more and more when our project's code base grew over million+ lines. I have to put it on the web one more time just in case there is a developer out there haven't seen this.

Basically you can set the compiler to generate compilation errors when there are enum values not covered in switch block. So that when a new value is added to an enum class, every switch statement on this enum class will get a compilation error. This way it is easier to find and add corresponding cases, and it is impossible to ignore them. :)

To make this work, you have to do (or not do) two things:

  1. DO NOT use "default" case when you do switch on enum values.
  2. Set your compiler to generate ERROR instead of ignore enum values not covered in switch. In eclipse (the greatest IDE), it is here:

Thursday, February 25, 2010

hey gmail, what is up? I am a little worried about you.

I am a loyal fan of gmail ever since its first beta version. I jumped all over the web to get an invitation back in 2004. A couple years ago when my geeky friends switched their companies' email system to gmail, I was a supporter too.

But a couple things happened recently, which made me not so sure no more. Starting later last year I began to receive emails sent to another address that I had no relation with. Apparently gmail is forwarding others email into my inbox, all junk though. But if it is doing this, how do I know it is not forwarding my email to others?

Another hiccup happened this week. I pop emails from several alias accounts. And mails start showing up late, 2 or 3 days late, as a consequence I wasn't able to get back to people in time.

Google pretty much runs my life. I wish the best for them. But with the buzz burns still all over the web, this kind of misbehaviours on email application is worrisome. Even the giant can bite more than it can chew, sometimes. So this is a shout out to the gmail team, check it out.

Tuesday, February 16, 2010

JBoss Tools Eclipse plugin content assist lessons

I learned a couple lessons in the process of making JBoss Tools Eclipse plug-in to do content assist (where you type ctrl-space to get code suggestions). This is mostly about JSF component libraries.

First of all, in order to get content assist for JSF components, you must have the TLD file in your class path. We are using Richfaces libraries, and all its components get content assist. The ones I had trouble with are jsf-core and jsf-html, the f: and h: tags by default. Oddly enough though, with all the JSF libraries I have on my class path, none of them had packaged the TLDs for these two. I made a jar file and put jsf-core.tld and jsf-html.tld in the META-INF folder to get this working.

Another lesson learned is that the name space URI has to match exactly with the TLD's. I mean exactly. Kind of obvious, but easy to miss when trouble shooting. I added Openfaces like this xmlns:o="" in the first place and it didn't work, because it is expecting a "/" at the end. So the correct syntax is xmlns:o=""

Sunday, February 14, 2010

Build Synergy in a new ubuntu system

Had to jump through quite a few hoops to get synergy to compile on a fresh ubuntu 9.10 box, including gcc-cpp and libxtst stuff. The most annoying error message is the 'You must have the XTest library to build synergy' one, it just won't go away after I got all the xtest, libxtst, libxest, libxcb-xtest crap installed.

Well, here is all it takes: (copied from synergy-plus)

By the time I figured this out, I already moved on to synergy-plus, which is much easier.