• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

What's the answer to this question?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following class, are the two functions serv1 and serv2 thread safe?




My question is this - if the JVM ensures that a particular operation will be atomically executed, then does it make a function that contains only that operation thread safe? Afterall, only one thread will be executing that operation if the JVM will be executing it atomically, right?

(I dont know how right it is, but the person who asked me this question said the answer is serv1 is thread safe but serv2 isnt)
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Neha Gap:
if the JVM ensures that a particular operation will be atomically executed, then does it make a function that contains only that operation thread safe?

It depends on the compiler and the rest of the code. In most cases it's not enough that the value is written atomically, in order to guarantee thread safety.
 
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on what you declare as synchronized
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(I dont know how right it is, but the person who asked me this question said the answer is serv1 is thread safe but serv2 isnt)



This is one of those academic questions... Basically, neither i or j is actually used, so does it really matter if it is actually threadsafe? And if it will be used then the answer is dependent on how it will be used.

But... if threadsafe means to store it correctly, then this person is correct. The serv1 method will store the variable correctly, while serv2 may not.

The reason is, for 64 bit primatives, which are not volatile, the JVM will not guarantee the write to be atomic. For 32 bit primatives, it is guaranteed to be an atomic write (or read).

Henry
 
Neha Gap
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, one would expect that all primitives would behave the same way, but this makes sense.

Had one more question.. how does it matter how the values are read? Say, I had two other methods, getI() and getJ()which were or were not synchronized. How would that matter while deciding whether or not serv1 and serv2 are thread safe?

Thanks for the response!
Neha.
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic