Think about how another thread would be able to access the object on which the locking takes place. (This could be a rather borderline case, the string you are using will be in the string pool, so perhaps another thread could just access the same object. There's probably no way you could be certain because whatever happens is implementation dependent.) So use an object which is explicitly shared between threads. Don't forget that we lock shared objects, not the variables referring to them. [ May 12, 2004: Message edited by: Barry Gaunt ]
Barry, You have an excellent point about the synchronization object being in the string literal pool. I got curious and wrote a test program to try it out. And you are 100% correct! All the threads are synchronizing on the same object. That is an excellent catch!
How do they get the deer to cross at the signs? Or to read this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop