Using the volatile keyword in Java.
“The volatile modifier is used when you are working with multiple threads.
The Java language allows threads that access shared variables to keep private working copies of the variables; this allows a more efficient implementation of multiple threads. These working copies need be reconciled with the master copies in the shared main memory only at prescribed synchronization points, namely when objects are locked or unlocked. As a rule, to ensure that shared variables are consistently and reliably updated, a thread should ensure that it has exclusive use of such variables by obtaining a lock that, conventionally, enforces mutual exclusion for those shared variables. “
Careful, volatile is ignored or at least not implemented properly on many common JVM’s, including (last time I checked) Sun’s JVM 1.3.1 for Windows. There was an article on DDJ that could demonstrate this even on single-processor machines–the article is locked now but you can still download the code listing:
http://www.ddj.com/ftp/2002/2002_06/jqa0602.txt
This site is extremely well to get answer for the common FAQs in java.
I want to know about keyword at java. please give me about that information.
thanks
What is the difference between transient and volatile keyword.when we should use transient and when volatile?
From http://java.sun.com/docs/books/tutorial/java/javaOO/variables.html
“The volatile keyword is used to prevent the compiler from performing certain optimizations on a member.”
According to Joshua Bloch’s excellent book “Effective Java Programming Language Guide”, “… the volatile modifier guarantees that any thread that reads a field will see the most recently written value.”
“The transient marker is not fully specified by The Java Language Specification but is used in object serialization which is covered in Object Serialization to mark member variables that should not be serialized.”
I’d suggest reading chp. 10 of “Effective Java Programming Language Guide”, which speaks to serialization and the use of the transient keyword.
if u declare a variable as volatile.then is it equivalent to say that when a thread tries to use it, then it’ll obtain a lock on it so that the other thread cant write into it until the lock is released by the first one??
if you declare a variable as volatile it will always read from and write to main memory – some threads are able to keep copies of shared variable values, but if these are not synced with the master copies, then data gets messed up. Volatile means the JVM always keeps checking main memory, rather than private thread records.
Can any one tell the what is exact use of volatile
keyword in java
The volatile modifier requests the Java VM to always access the shared copy of the variable so the its most current value is always read. If two or more threads access a member variable, AND one or more threads might change that variable’s value, AND ALL of the threads do not use synchronization (methods or blocks) to read and/or write the value, then that member variable must be declared volatile to ensure all threads see the changed value.
if u use transistent keyword in ur code that varaiable can’t be serilaized ,when transferring data to remote m/c it will go as null.
I need to know the exact meaning of volatile keyword in java
I need to know the exact meaning of volatile and transient keyword in java and where to use them
Transient… Transient keyword tells they are not part of the persistent.Suppose variable is declared as transient they are not serialized (or)persisted . They are not part of the persistent state of an object.
Volatile… A volatile modifier is mainly used in mutiple threads. Java allows threads can keep private working copies of the shared variables(caches).These working copies need be updated with the master copies in the main memory.
But possible of data get messed up. To avoid this data corruption use volatile modifier or synchronized .volatile means everything done in the main memory only not in the private working copies (caches).( Volatile primitives cannot be cached ).
So volatile guarantes that any thread will read most recently written value.Because they all in the main memory not in the cache…Also volatile fields can be slower than non volatile.Because they are in main memory not in the cache.But volatile is useful to avoid concurrency problem.
What is the full meaning of System.out.println?
When I have 2 tread accessing the same variable, one of them has only write access the other one only read access do have to use volatile?
I other words: Can I be sure that the reading thread get informed that the other thread changed the variable (needn’t be immediately) or can I get an endless loop when the reading thread waits for a change by the other one?