Until now, I was not worried about how volatile works in details until I see the following question in K&B's Master Exam #2.
According to K&B everything we need to know for the exam about volatile is that volatile can be applied only for variables. Not for methods or classes.
So my question is : What should I really know about volatile for the exam ?
Which are true ? (Choose all that apply)
a) To implement java.io.Serializable, a class must implement two methods.
b) When an object is serialized, if it has references to other objects, those objects must be serializable.
c) A serialized object can be deserialized only by the same JVM which was used to serialize it.
d) The value in instance variables market volatile will not survive serialization and deserialization.
The correct answer is B, but I think B is not correct because if an object supposed to be serialized has references to other objects, such references may be marked as transient. So, such references must NOT be serializable.
May be I mislead this question. Can anyone please explain ?
"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
At first it looks like a question about serialization more than a questions about multithreading.
First, I share your assessment: saying that answer B is correct implies the supposition that none of those references are transient. So this looks like a wrong question to me.
The option d seems to be an answer intended to see if you confuse volatile with transient.
Now transient implies that field member will not be serialized. Volatile is related to multithreading.
Volatile is related to Java Memory Model, the threads are allowed to keep copies of their variables, if one thread modifies one this variables other threads may not realize of the change. Marking a variable as volatile implies that any reading or writing of the variable will be reconciled with the master copies of themselves.
Volatile also ensures that the reading and writing operations of the variables are done atomically. That means that two thread will not write the same variable concurrently, but one after the other.
This is very important for variables bigger than 32 bits, like long or double, because they would be treated as two reads or writes of 32 bits. With volatile, 64-bit writes and reads will be enforced.
Another important issue related to volatile variables is that fact that some processors may execute the sentences in different order than that expected by the developer, as long as it does not alter the final result. Many modern processors can do this to optimize code, however, it may have implications on multithreading. Volatile enforces that the operations will be executed as programmed.
Finally, it is important to understand that, although a single read and single write will be enforced to be atomic operations , a combination of both won't. Hence incrementing a variable (i++) implies reading and writing the variable. After the reading the variable could be incremented by another thread before this thread increments it. This kind of behavior has to be enforced with Synchronization or with the atomic variables now present in Tiger. [ April 14, 2006: Message edited by: Edwin Dalorzo ]
Joined: Jan 19, 2006
Wow Edwin ! Great explanation.
Thank you for your prompt replies and for making efforts to clarify this question for me as much as you can.
Thanks Arno, I've noticed your full participation here. Thank you very much.
It's always good to know that once you put your doubt at JavaRanch, in a few minutes you have the right answers. [ April 14, 2006: Message edited by: Edisandro Bessa ]
subject: What should I know about volatile for the exam ?