Article View: borland.public.jbuilder.java.language
Article #7473Re: Nature of synchronized{ } (thread newbie)
From: Zoltan Grose
Date: Wed, 20 Jun 2001 21:19
Date: Wed, 20 Jun 2001 21:19
167 lines
5312 bytes
5312 bytes
Followup question to your examples:
Are you saying that this code snippet:
> public synchronized void setTemp2(int temp2) {this.temp2 = temp2;}
> or
> public void setTemp2(int temp2) { synchronized {this.temp2 = temp2;}}
>
> with
>
> public int performCalculation(int x, int y) {
> int temp1 = 0;
> temp1 = y + 5;
> synchronized
> {
> temp2 = x / 3;
> return temp1 + temp2;
> }
> }
is equivalent to this code snippet:
> Also
> public int performCalculation(int x, int y) {
> int temp1 = 0;
> temp1 = y + 5;
> synchronized(temp2)
> {
> temp2 = x / 3;
> return temp1 + temp2;
> }
> }
In the first example you have 2 sets of synchronized block. In the second
you have just one synchronized block, but referring to the temp2 variable.
I'm reading this as meaning if I write synchronized(temp2){} that I don't
need to put synchronized in setTemp2. Was that your intent?
-zoltan
"dnoyeB" <lamont_gilbert @ rigidsoftware.com> wrote in message
news:3b3110f0$3_1@dnews...
> No that is not a sufficient solution. Synchronized just means 2 threads
> cant call that code at the same time. However, you failed to synchronize
> the method below. Remember, you are synchronizing with respect to temp2
> actually.
>
> public void setTemp2(int temp2) {this.temp2 = temp2;}
>
> AS a result it can be called anytime, even while another thread is in the
> synchronized section you created below
>
> > synchronized {
> > temp2 = x / 3;
> > return temp1 + temp2;
> > }
>
>
> In otherwords, 2 threads cant enter the synchronized section you created
> which does add necessary safety, but 1 thread could be in that section,
> while another is calling setTemp2, which would again be thread unsafe.
>
> better is
>
> public synchronized void setTemp2(int temp2) {this.temp2 = temp2;}
> or
> public void setTemp2(int temp2) { synchronized {this.temp2 = temp2;}}
>
> with
>
> public int performCalculation(int x, int y) {
> int temp1 = 0;
> temp1 = y + 5;
> synchronized
> {
> temp2 = x / 3;
> return temp1 + temp2;
> }
> }
>
>
> Also
> public int performCalculation(int x, int y) {
> int temp1 = 0;
> temp1 = y + 5;
> synchronized(temp2)
> {
> temp2 = x / 3;
> return temp1 + temp2;
> }
> }
>
> good luck
>
>
>
>
> Synchronizing is taxing. Try not to synchronize in loops whenever
> possible!!
>
> "Zoltan Grose" <zgrose@pacbell.net> wrote in message
> news:3b1b28df$1_2@dnews...
> > I'm going to be diving into servlet programming shortly and I feel now
is
> a
> > good time to wrap my brain around threads and the keyword synchronized.
> >
> > From the reading I have done and the examples I have seen, I believe I
can
> > summarize the use of synchronized in this way:
> >
> > "If a method accesses (either reads from or writes to) a variable that
one
> > reasonably expects not be constant and that variable is not local to the
> > method, the accessing code should be synchronized."
> >
> > For example, I believe this code to be thread-safe since all of the
> > variables are local.
> >
> > public class MyClass {
> > public int performCalculation(int x, int y) {
> > int temp1 = 0; int temp2 = 0;
> > temp1 = y + 5;
> > temp2 = x / 3;
> > return temp1 + temp2;
> > }
> > }
> >
> > I believe this code to be thread-unsafe since temp2 is not local to the
> > method and could potential be modified at any point during any execution
> of
> > performCalculation.
> >
> > public class MyClass {
> > int temp2 = 0;
> > public void setTemp2(int temp2) {this.temp2 = temp2;}
> > public int performCalculation(int x, int y) {
> > int temp1 = 0;
> > temp1 = y + 5;
> > temp2 = x / 3;
> > return temp1 + temp2;
> > }
> > }
> >
> > The second example would be made thread-safe again by wrapping the
unsafe
> > section in a sychronized block.
> >
> > synchronized {
> > temp2 = x / 3;
> > return temp1 + temp2;
> > }
> >
> > Am I on the right track? (Am I in the right newsgroup?) My background is
> in
> > VB, so threads are one of the new concepts I'm trying to deal with
before
> I
> > get to deep. Thanks in advance.
> >
> > -zoltan
> >
> >
> >
>
>
Technical Details
Message-ID:
<3b31134f$1_2@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!newsfeed00.sul.t-online.de!t-online.de!news1.dtag.de!news.stgl.sel.alcatel.de!news.stgl.sel.alcatel.de!dnewsReferences:
<3b1b28df$1_2@dnews> <3b3110f0$3_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,ec3ad0c2eb0c6632
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-06-20 14:37:15 PST
From: "Zoltan Grose" <zgrose@pacbell.net>
Newsgroups: borland.public.jbuilder.java.language
References: <3b1b28df$1_2@dnews> <3b3110f0$3_1@dnews>
Subject: Re: Nature of synchronized{ } (thread newbie)
Date: Wed, 20 Jun 2001 14:19:38 -0700
Lines: 169
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
NNTP-Posting-Host: 63.193.228.78
Message-ID: <3b31134f$1_2@dnews>
X-Trace: dnews 993071951 63.193.228.78 (20 Jun 2001 14:19:11 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!newsfeed00.sul.t-online.de!t-online.de!news1.dtag.de!news.stgl.sel.alcatel.de!news.stgl.sel.alcatel.de!dnews