Re: synchronized block question...
Ugh... this starts looking kinda ugly, what spurious bugs one may run
Same thing if I assign an empty string to a variable and use it for
locking, I guess?
So a safer method would be to use a value like: new Date()
for semaphore, that is not likely to get covertly duplicated-by-
caching by the JVM then...?
Anything explicitly constructed via new is safe in that two variables
constructed in this manner are guaranteed to be different in terms of
You generally can't assume that anything else is safe, especially:
* Autoboxed objects. These are cached for the values [-128, 127] for
bytes, shorts, ints, and longs; booleans cache [true, false], characters
[0, 127], and doubles and floats none.
* String literals. Every literal "A" is the same reference (also the
same as their interned representations).
* Interned strings. These are defined to be the same reference.
These are not safe in the sense mentioned above, but they are useful as
* Enum values. An enum contains the only instances.
* Class literals.
If you want a semaphore, you might look at Java's semaphore:
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth