Article View: borland.public.jbuilder.java.language
Article #7471Re: Nature of synchronized{ } (thread newbie)
From: dnoyeB
Date: Wed, 20 Jun 2001 19:38
Date: Wed, 20 Jun 2001 19:38
117 lines
3970 bytes
3970 bytes
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:
<3b3110f0$3_1@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!newsfeed01.sul.t-online.de!t-online.de!dnewsReferences:
<3b1b28df$1_2@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:16:20 PST
From: "dnoyeB" <lamont_gilbert @ rigidsoftware.com>
Newsgroups: borland.public.jbuilder.java.language
References: <3b1b28df$1_2@dnews>
Subject: Re: Nature of synchronized{ } (thread newbie)
Date: Wed, 20 Jun 2001 15:38:25 -0400
Lines: 119
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
NNTP-Posting-Host: 63.68.196.49
Message-ID: <3b3110f0$3_1@dnews>
X-Trace: dnews 993071344 63.68.196.49 (20 Jun 2001 14:09:04 -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!newsfeed01.sul.t-online.de!t-online.de!dnews