Article View: borland.public.jbuilder.java.language
Article #15065Re: Syncrhonise this or that
From: Steve Horsley
Date: Thu, 15 Jul 1999 00:00
Date: Thu, 15 Jul 1999 00:00
45 lines
1726 bytes
1726 bytes
Your first method_x is synchronized in its entirety. Only one thread
can be executing it at a time - other threads that try will be blocked
until the first thread finishes.
The second method_x synchronizes on the same object, but only for
do_b(). Multiple threads can be in the do_a() part of the function
at once, with the attendant possibility of confusing each other. Only
one thread at a time can get into the do_b() method however. Other
threads will have to wait their turn after finishing do_a().
I feel that the first approach is better for small methods, but that
larger methods where only a small section of the code actually needs
protecting from multi-threading are better off using the second
approach.
Hope this helps, Steve.
//---------------------------------
Hugo Ferreira wrote:
>
> Hi,
>
> I have a basic question about thread syncronization.
> What is the differenece between these two calls:
>
> void syncrhonized method_x()
> {
> do_a();
> do_b();
> }
>
> and
>
> void method_x()
> {
> do_a();
> syncrhonized( this )
> {
> do_b();
> }
> }
>
> TIA.
>
> Hugo Ferreira.
Technical Details
Message-ID:
<378D9F10.AA965F22@cwcom.co.uk>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7mk5dd$4sn7@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,883d3e7766e8b829 X-Google-Attributes: gidf74b3,public From: Steve Horsley <steve.horsley@cwcom.co.uk> Subject: Re: Syncrhonise this or that Date: 1999/07/15 Message-ID: <378D9F10.AA965F22@cwcom.co.uk> X-Deja-AN: 501259438 Content-Transfer-Encoding: 7bit References: <7mk5dd$4sn7@forums.borland.com> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: Cable & Wireless Communications Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language