• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Returning 2 separate variable as array will ensure valid read?

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


Does the method get() ensures valid read of both values in multi-threaded environment?
If it does, does this mean when constructing an array, JVM silently locks both x and y?
I thought that during construction of the array, JVM will perform following steps:
1. read x
2. populate array with x
3. read y
4. populate array with y
5. return the populated array

during the short interval between step 2 and 3, it's possible that another thread calls set() that will change the value of y. this will make step 3 retrieve the wrong y value, rendering the coordinate to be invalid as a whole. Could anyone please explain in more detail in this matter?
thanks
 
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Does the method get() ensures valid read of both values in multi-threaded environment?



Depends on how you define valid. If by valid you mean that the most recent writes to the int variables are always seen, you need to synchronize access to them or apply some memory barrier in order for that to happen.
Your code just has setters and getters and race conditions are specific to how you are accessing a critical resource. When you say multi-threaded environment, there are specifications applicable there as well so we can assess what race conditions apply. But in general, access to the int variables here is not thread safe cause you might be reading stale/cached values.

If it does, does this mean when constructing an array, JVM silently locks both x and y?


That class is not thread safe. So this doesn't apply. I'm not sure about JVM being able to silently lock anything without being instructed to. I don't think that is possible.

I thought that during construction of the array, JVM will perform following steps:
1. read x
2. populate array with x
3. read y
4. populate array with y
5. return the populated array



I can't say for sure if that is going to be the order cause that is really a very specific implementation detail. I don't know if JVM reads every variable first and then does the writing to the array or if it reads and writes one int at a time. But as far as your question that follows ( in the quoted text below ) is concerened,

during the short interval between step 2 and 3, it's possible that another thread calls set() that will change the value of y. this will make step 3 retrieve the wrong y value, rendering the coordinate to be invalid as a whole. Could anyone please explain in more detail in this matter?



yes, it is very much possible regardless of what the order is. Read of x or y or both could read stale values, hence the array can have stale values.

Chan.
 
It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic