aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Use of Volatile keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Use of Volatile keyword" Watch "Use of Volatile keyword" New topic
Author

Use of Volatile keyword

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

hi ,

Can anyone explain the volatile keyword and it's use (in real life).
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Hello ragi.

I never seen volatile in use, but i found this link. It might help you.

http://www.javamex.com/tutorials/synchronization_volatile.shtml


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

See below code.

Volatile means each Thread Access the variable will have its own private copy which is same as original one.
But if the Thread is going to change that private copy,then original one will not get reflected.

see below code.




SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

same thing can be achieved through synchronization also

Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

That's not correct, IIRC. Volatile means the variable's value will never be cached thread-locally, but all reads and writes will go straight to main-memory, and the variable acts as though it is synchronized on its reference (though it isn't really, it cannot block and it can be null). What it does mean is, for example, when you start setting an integer using the actual variable, and just while you're setting that variable, some other thread tries to set that variable to another value, your operation will run first, it is atomic. If it hadn't been volatile, something could sneak in.

Basically, getting it is atomic, setting it is atomic. They are each atomic, but not together; however, there is a way to do that since Java 5.

an example of volatile in regards to caching:

If the variable were not declared volatile (and without other synchronization), then it would be legal for the thread running the loop to cache the value of the variable at the start of the loop and never read it again. If you don't like infinite loops, this is undesirable.

* volatile is not necessary– or in fact possible– for fields that are immutable (declared final);
* volatile is not necessary for variables that are accessed by only one thread (though of course you have to make a correct decision that they are only accessed by one thread!);
* volatile is not suitable for complex operations where you need to prevent access to a variable for the duration of the operation: in such cases, you should use object synchronization or one of Java 5's explicit lock classes added.

When should you use volatile? For instance variables that are constantly changed by more than one thread.

source: http://www.javamex.com/tutorials/synchronization_volatile.shtml


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

The explaination provide by me is after studying through JLS Third Edition.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

JLS Third Edition states: http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.1.4

Which is quite different from what you said, I think.

Also, in your example, a volatile variable would not be necessary, because there is only one thread running, so there will be no concurrent access.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Thanks Dieter Quickfend for clearing it.now i understand that
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

below link is also useful
Volatile keyword
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Keep in mind - volatile is not an equivalent to synchronized! Synchronized uses locks & is capable of blocking a thread, but volatile does not have the ability to block - so you are not really thread-safe when you use it. As Dieter said, it just writes/reads from the main memory rather than the cached copy. Simple.
You can use volatile on even primitives, but synchronized cannot be.

OCPJP 6
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Vinoth Kumar Kannan wrote:...you are not really thread-safe when you use it...

Sorry about that info guys...I was wrong!
I took a bit deeper dive into volatile/synchronization and here is what I got..
Only in the Old JMM(Java Memory Model) reads/writes of volatile variables were allowed to be reordered with other non-volatile reads/writes - so not much reliable, but with the new JMM each read/write to a volatile field is like "half" a synchronization -- a read of a volatile has the same memory semantics as a monitor acquire, and a write of a volatile has the same semantics as a monitor release.
As a result of making volatile more "heavyweight," the performance cost of using volatile has been brought closer to the performance cost of synchronization in some cases, but the performance cost is still quite low on most platforms.

source=http://www.ibm.com/developerworks/library/j-jtp03304/
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10285
    
169

This has been discussed many times before. Here are some of those discussions:

http://www.coderanch.com/t/385237/java/java/Volatile-Modifiers#1682610
http://www.coderanch.com/t/497771/java/java/volatile-variables

[My Blog] [JavaRanch Journal]
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Thanks, Jaikiran Pai.
Actually, I said something I wasn't really sure of. Mine seemed to be the last post(no one opposing/correcting thereafter..), and when I read more on volatile & synchronization, I realized I was wrong and did a small update, so that folks in future who see this wrong post should not take it as something right - that being my intention actually.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10285
    
169

Vinoth Kumar Kannan wrote:
Actually, I said something I wasn't really sure of. Mine seemed to be the last post(no one opposing/correcting thereafter..), and when I read more on volatile & synchronization, I realized I was wrong and did a small update, so that folks in future who see this wrong post should not take it as something right - that being my intention actually.


No problem
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of Volatile keyword