• 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

What should I know about volatile for the exam ?

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

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 ?

Question :

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 ?
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ( me again lol),
normally you should only know 'volatile' is for instance variable...i passed scjp and i didn't have any problem with volatile.

arno
 
Arno Reper
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ps : more explaination about volatile
http://www.javaperformancetuning.com/news/qotm030.shtml

it look like synchronized but for instance variable and not blocks...i think, haven't read all the articles
[ April 14, 2006: Message edited by: Arno Reper ]
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Edisandro.

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 ]
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic