"Mr.Bad" <[EMAIL PROTECTED]> writes:

> Hey, so, just wondering here, for my own edification: is there an
> advantage to doing this:
> 
>         public void gar(int foo) {
>             synchronized(this) {
>                 // stuff happens here;
>             }
>         }
> 
> ...rather than this:
> 
>         public synchronized void gar(int foo) {
>             // stuff happens here;
>         }
> 
> ? The first is an idiom I've run across a few times in Fred code, and
> I've never seen it used before.


Those two chunks of code are functionally identical (and chances are
they'll be rendered identically in bytecode as well).  The latter is
better style, IMHO, because it makes clear from an API perspective
that gar() will be locking.  I try to avoid using the "synchronized()
{}" construct.

There is, of course, a significant and worthwhile difference between
this:

        public synchronized void gar(int foo) {
                // stuff happens here;
                // atomic stuff happens here;
        }

and this:

        public void gar(int foo) {
                // stuff happens here;
                synchronized(this) {
                        // atomic stuff happens here;
                }
        }

, and the latter is better.  I would prefer to see the synchronized
portion of gar() in a separate method, though.

-S

_______________________________________________
Freenet-dev mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/mailman/listinfo/freenet-dev

Reply via email to