David Holmes wrote:
> So while the example is correctly synchronized by the definition, it is
> non-intuitively correctly synchronized in my view. I really would hate to
> start seeing people writing "clever" code this way. The non-use of direct
> sync isn't even an optimisation in this case as far as I can see - and the
> program would behave no differently if the accesses to w were moved to
> inside the sync blocks.
If it didn't work consider how much code would break. Pretty much
anywhere that passes data between threads.
Too much synchronization is bad too, particularly holding a lock while
calling methods that you aren't sure what will do.
For instance, a few years ago I was checking that an applet failed to
print under the Java Plug-In. To my surprise it deadlocked. The print
was requested from a thread handling socket input. The Plug-In attempts
to wait and open a confirmation dialog box in the AWT event dispatch
thread. The AWT activity caused the applet to attempt to gain the same
lock as the original thread. Deadlock.
> In short I wish this were not correctly synchronized so that I could easily
> justify telling people not to do it. ;-)
That's almost equivalent of telling people not to write multithreaded
code. Even EventQueue.invokeLater is right out. Considering how well it
is understood by most professional Java programmers, probably a good idea.
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel
This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:58 EDT