Tuesday, October 14, 2003

Using synchronized in Java

For quite awhile, I've been using the sychnronized keyword in an incorrect manner, probably because of my ColdFusion background. Synchronization locks in ColdFusion lock access to a block of code based on some arbitrary lock phrase. As long as two locks have the same lock phrase, they single-thread the process through the protected code. The concept that I recently relearned was that Java sychronizes on an object - typicallly 'this'. Which has some interesting ramifications when one object starts two threads which both refer to static objects. The parent can have every method sychronized without affected the child threads. The above issue is relative mundane, but my blindness to the true nature of Java synchronization shows how important it is to constantly reexam even your most basic understandings about the tools that you use everyday.
Post a Comment