Article View: borland.public.jbuilder.java.language
Article #7666Re: Nature of synchronized{ } (thread newbie)
From: Torsten Welches
Date: Mon, 04 Jun 2001 11:02
Date: Mon, 04 Jun 2001 11:02
66 lines
2944 bytes
2944 bytes
Zoltan,
yes, you are on the right track.
Your examples are all fine except that your second one needs one more
synchronization around the setTemp2 method. All read/write access of shared
data must be guarded to get effective synchronization.
In case you didn't see this tutorial yet:
http://web2.java.sun.com/docs/books/tutorial/essential/threads/index.html
Torsten
Zoltan Grose schrieb in Nachricht <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:
<3b1b6a78_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!news.maxwell.syr.edu!news-ge.switch.ch!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-04 04:14:19 PST
From: "Torsten Welches" <torsten.welches@who.cares.netcologne.de>
Newsgroups: borland.public.jbuilder.java.language
References: <3b1b28df$1_2@dnews>
Subject: Re: Nature of synchronized{ } (thread newbie)
Date: Mon, 4 Jun 2001 13:02:26 +0200
Lines: 68
X-Newsreader: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
NNTP-Posting-Host: 195.14.244.69
Message-ID: <3b1b6a78_1@dnews>
X-Trace: dnews 991652472 195.14.244.69 (4 Jun 2001 04:01:12 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!news.maxwell.syr.edu!news-ge.switch.ch!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews