• 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

Why volatile variable can't be serialized?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why volatile variable can't be serialized?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"jkith"

Welcome to JavaRanch!

Can you please update your display name so it complies with our Naming Policy. You can do this here.

Thanks!
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jkith,

For you, why would one serialize a variable?

and,

Again for you, why would one specify a veriable as volitile?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please reserve this forum for truly advanced questions. Moved to Java in General (intermediate).
 
subbu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In multithreaded environment to maintain the unique value of a particular field, we are using volatile.

Any reason why it can't be serialised.

If a particular memeber(field) declared as transient it will not be included while doing the serialization.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"subbu"

Thank you for taking the time to update your display name. Unfortunately a valid display name must have both a real-sounding first and last name. Please can you update it again. Thank you.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know, and I have just skim-read the "volatile" section of the language spec and the introduction to the Serialisation spec, the "volatile" keyword has no effect on serialisation.

The "volatile" keyword, as you say, is about maintaining a consistent view of the field between threads. It is "transient" that controls serialisation. I could find no evidence of any link between the two keywords.

Did you actually try to serialise your volatile field? I would expect it to succeed.

Note that you can serialise even transient fields. It's only the *default* serialisation mechanism that does not serialise them. If you provide custom serialisation, which ain't too hard, you can put anything you like in the serialisation stream.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Its a way to maintain the resources held by runtime objects.

Assume, you are using one file stream to read data in an object which you want to serialize.
Will it work after deserialization on other machine?. may or may not.

So all network,file,i/o,DB resources variables mainly used as transient variables, so that they will be reset to the runtime environemnt in which the object is deserialized.

Note: ofcourse to relase the resources too ...

More inputs are welcome.
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic