aspose file tools*
The moose likes Threads and Synchronization and the fly likes Can threads cache writes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Can threads cache writes" Watch "Can threads cache writes" New topic
Author

Can threads cache writes

Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 420
    
    2

Sorry another dull question I'm using a third party open source library used by lots of people so I'ed assume its right and I'm wrong but they appear to use non volatile member variable and synchronize the reads but not the writes, sometimes neither which is obviously wrong . Now I'ed NEVER write code like this regardless of if it works or not but i have to decide if its an issue i..e if it'll always work I'll have to live with it but if its a potential bug I'll raise it with them but I'ed like to be sure of my facts.

The following code is used by multiple threads (all variables used in this way are atomic)

Hope that makes sense, any help appreciated.
[ October 26, 2006: Message edited by: Jim Yingst ]

"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Chris, I added code tags to your post for readability.

In the code shown, your concerns are justified. Nothing forces the values writeen by unsynchronized code to be updated in main memory - which means the other threads (even using synchronized code) may still see older, out-of-date data. For synchronization to work correctly, you need to synchronize all access to the mutable data.

Sometimes you will see people do the opposite of what is done here - they will synchronize writes but not reads. That approach is equally flawed if you want to have up-to-date information about the contents of the data. Unfortunately, people sometimes do it anyway; I'm not sure why. It seems to be a popular myth, the idea that you should synchronize writes but not reads.
[ October 26, 2006: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 420
    
    2

Thanks that was very useful much appreciated.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Chris Hurst:
snipped...library used by lots of people so I'ed assume its right and

My favorite method is to take some Phd that just got out of Phd school and sail in with a really penetrating question.

I get tore up often enough to appease my critics, but I once instructed a board certified attorney In the are of law in which they were board certified
With your demonstrated real-world expertise, it is prima-facia that you know the engineering expansion of assume - http://www.around.com/ariane.html - assumes the coders were at fault.

C++ Essentials - Cay S. Horstmann - who should know something about it - provides us with a basis for invoking the engineering definition of assume for the around website.

Because of the nature of and way that it is used, Java fora are legitimate places to question large-scale ignorance ... a contemporary topic in A.I.

I write code like this regardless who told me to or not, but i have to throw it away before lots of people decide its an issue.

It looks to me like you are going to have an unplesant meeting.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can threads cache writes