Friday, August 7, 2009

Multithreaded code can be a source of pitfalls, but I've only rarely seen with problems with threading. Mostly because the vast majority of code don't have to deal with or be aware of multiple threads, and the code that does is generally written by people who know what they're doing.

The worst example I've seen was some code using a static variable to build a string. I'm sure it worked fine when it was first tested. Under more load, though, it generated these gigantic strings that mixed information from multiple transactions.

In some code by someone who I assume generally knew what he was doing, there was a comment that said synchronization wasn't needed because the JVM does operations on longs atomically. I didn't change anything, but I'm pretty sure the JVM specification explicitly does not guarantee that operations on longs were atomic. I suppose they could be atomic on modern systems.

About 10 years ago, one guy said that he made some methods synchronized because he couldn't figure out what some problem was and thought that making the methods synchronized might help. I don't think it did, though.

No comments:

Post a Comment