Basically yes. Much less code than you normally would think needs to be declared synchronized. Maybe someone can post a link to a good guide to thread synchronization - personally I only can recommend the O'Reilly Java Thread book but that's personal taste(!)
However - as you said, synchronization implies a performance penalty as there will be some code around the synchronized block for lock checking etc. This is regardless of where you put the synchronized flag - a method is just as well a code block. So make sure not to change a single large synchronized block (such as a whole method) for a patchwork of small synchronized blocks. You might find your thread spending the gained time (for not having to wait for locks to become free) in synchronization management code. Tobi ----- Original Message ----- From: "Erik Price" <[EMAIL PROTECTED]> To: "Tomcat Users List" <[EMAIL PROTECTED]> Sent: Wednesday, January 29, 2003 9:26 PM Subject: Re: about singletons (ot) > So you mean that the original author (mike jackson) was saying that he > used synchronized code blocks to apply a finer level of detail in > specifying what is synchronized and what isn't, as opposed to just > declaring an entire method synchronized? > > I understand that synchronization implies a performance penalty, but I > wasn't sure what the advantage to using synchronized blocks over > synchronized methods was. > > > Erik > > > > > Tobias Dittrich wrote: > > The reason why you don't want to use synchronized methods is that a > > synchronized block can only be executed by one thread at a time. Every other > > thread wanting to access this method will be blocked during this time (well, > > basically). So you want to try to keep the synchonized blocks as small as > > possible. > > > > Having said that I wonder weather performance is an issue in the singleton > > vs only-static discussion. Is there a significant difference in execution > > speed? After all one has to make one additional method call every time when > > accessing a singleton method (the getInstance() which is synchronized, > > too). And since we're off topic anyway: is a call to a static method faster > > than a "normal" one to an object (well, I mean the overhead from the method > > call, not the execution speed of the method body ... )? > > > > > > Cheers > > Tobi > > > > > > > > From: "Erik Price" <[EMAIL PROTECTED]> > > To: "Tomcat Users List" <[EMAIL PROTECTED]> > > Sent: Wednesday, January 29, 2003 1:46 PM > > Subject: Re: about singletons (ot) > > > > > > > >> > >>Mike Jackson wrote: > >> > >>>The difference is that if you use a singleton there's one instance. If > >>>everything > >>>is static then you only have one copy. Usually when you use a singleton > >>>it's to > >>>control access to some resource, the intent is that you use the > > > > singleton > > > >>>and some > >>>synchronized calls (note I don't mean synchronized methods, but > > > > synchronized > > > >>>code > >>>blocks) to control threads using that resource. > >> > >>Why could you not use synchronized methods? > >> > >> > >> > >>Erik --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]