• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Two threads accessing an object and caching?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If one thread frequently calls increment() and second thread frequently calls getValue(). Then what prevents the second thread from caching the count variable and thus returning an incorrect value?
 
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by 'cache'?

Here is some info on Java's threads in Java Language Specification's Chapter 17 (Threads and Locks, Synchronization and Memory Model): https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html
 
David Moose
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean by "cache" is that let's say second thread starts going first and executes the getValue a million times times and after that decides to not go to main memory but just get the value from CPU's register.
 
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Declare count as volatile
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Moose wrote:I mean by "cache" is that let's say second thread starts going first and executes the getValue a million times times and after that decides to not go to main memory but just get the value from CPU's register.



Lakshman Arun wrote:Declare count as volatile



Not needed. The synchronization will prevent the accessing of the count variable at the same time. And crossing of the synchronization barrier will flush the processor cache (along with any register copies, which BTW, is not the same as the processor caches).

Having said that, keep in mind that the getValue() method returns a copy of the value. This means that when the method releases the lock, the copy can be different from the original variable.

Henry
 
David Moose
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

David Moose wrote:I mean by "cache" is that let's say second thread starts going first and executes the getValue a million times times and after that decides to not go to main memory but just get the value from CPU's register.



Lakshman Arun wrote:Declare count as volatile



Not needed. The synchronization will prevent the accessing of the count variable at the same time. And crossing of the synchronization barrier will flush the processor cache (along with any register copies, which BTW, is not the same as the processor caches).

Having said that, keep in mind that the getValue() method returns a copy of the value. This means that when the method releases the lock, the copy can be different from the original variable.

Henry



Thanks!! So the act of locking in getValue() by second thread will synchronize memory/flush registers,L1,L2 etc Where one I find this in JLS https://docs.oracle.com/javase/specs/jls/se7/html/index.html

 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JLS only explains this in terms of happens-before relationships. How these relationships are implemented is platform dependent, although the Java VM specification probably says something about it.
 
David Moose
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/package-summary.html#MemoryVisibility
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic