Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why volatile variable can't be serialized?

 
subbu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why volatile variable can't be serialized?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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!
 
Steve Fahlbusch
Bartender
Posts: 602
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic