Article View: borland.public.jbuilder.java.language
Article #8618Re: volatile keyword - doesn't seem to make a change
From: John McGrath TeamB
Date: Mon, 12 Mar 2001 08:00
Date: Mon, 12 Mar 2001 08:00
36 lines
2238 bytes
2238 bytes
On Sat, 10 Mar 2001 22:43:52 +0200, Yonatan Taub wrote:
> As I understand, threads create a local copy of variables which
> allows them to execute faster. They retrieve the master copy
> only after exiting or entering a synchronized block or the
> thread itself.
:
> The variable "value" was NOT declared volatile but it acts this
> way. If it wasn't volatile, the inner thread should have NOT
> noticed the change in variable "value" and would continue to
> execute indefinitly.
You are referring to the "memory barrier" that is guaranteed to exist
when a lock is released. There is some very interesting discussion of
memory barriers and their effect on synchronization in the following
articles:
http://www.javaworld.com/jw-02-2001/jw-0209-double.html
http://www.javaworld.com/jw-02-2001/jw-0209-toolbox.html
The reason that your code does not behave as you think it should is
that the "volatile" keyword imposes additional restrictions on how the
variable should be handled, but the absence of the "volatile" keyword
does not. In other words, the update to the variable from one thread
must be seen in the other thread if volatile is used. But the absence
of the "volatile" keyword does not guarantee that the update will not
be seen in the other thread.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID:
<MPG.151640292eee888e98a052@forums.borland.com>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!not-for-mailReferences:
<3aaa9320_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,c0d5023d960f16ab X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-03-12 00:00:07 PST Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!not-for-mail From: John McGrath [TeamB] <ng-b@JPMcGrath.net> Newsgroups: borland.public.jbuilder.java.language Subject: Re: volatile keyword - doesn't seem to make a change Date: Mon, 12 Mar 2001 03:00:36 -0500 Organization: Another Netscape Collabra Server User Lines: 36 Message-ID: <MPG.151640292eee888e98a052@forums.borland.com> References: <3aaa9320_2@dnews> NNTP-Posting-Host: dslcv1-60.fast.net X-Newsreader: MicroPlanet Gravity v2.30