Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: why are using volatile in java

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default why are using volatile in java

    Hi,
    i can't understand why are using volatile in java please explain with example
    Last edited by thirupathiswami017; May 24th, 2012 at 03:38 PM.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: why are using volatile in java

    The use of this keyword as part of dealing with thread interference and memory consistency errors is a discussed in the Synchronization section of Oracle's Tutorial. (It's in the last section on "Atomic Access", but the whole thing is worth reading. Also, perhaps, the surrounding chapter on "Concurrency").

    The Java Language Specification also discusses examples in the section 8.3.1.4 volatile Fields.

  3. #3

    Default Re: why are using volatile in java

    First, the easy cases where you basically don't need volatile or any other synchronization mechanism:

    1. volatile is not necessary– or in fact possible– for fields that are immutable.
    2. volatile is not necessary for variables that are accessed by only one thread.
    3. 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.

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Location
    United Kingdom
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: why are using volatile in java

    Volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache value of this variable and always read it from main memory. So if you want to share any variable in which read and write operation is atomic by implementation e.g. read and write in int or boolean variable you can declare them as volatile variable. From Java 5 along with major changes like Autoboxing, Enum, Generics and Variable arguments , Java introduces some change in Java Memory Model (JMM), Which guarantees visibility of changes made by one thread to another also as "happens-before" which solves the problem of memory writes that happen in one thread can "leak through" and be seen by another thread. Java volatile keyword cannot be used with method or class and it can only be used with variable. Java volatile keyword also guarantees visibility and ordering , after Java 5 write to any volatile variable happens before any read into volatile variable. By the way use of volatile keyword also prevents compiler or JVM from reordering of code or moving away them from synchronization barrier.

  5. The Following User Says Thank You to tayloralina For This Useful Post:

    sharadsri (December 19th, 2012)

  6. #5
    Junior Member
    Join Date
    Dec 2012
    Location
    India
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: why are using volatile in java

    Volatile is just a keyword in java. This can be used only with the variable declaration.
    Example : volatile int variable_name;
    - This indicate that the variable can be modified asynchronously by more than one thread.
    - For volatile variable, compiler guarantee that all the threads should see the same value of a specified variable.
    - The value of this variable will never be cached thread-locally, all reads and writes will go straight to main memory.

  7. #6
    Junior Member
    Join Date
    Dec 2012
    Location
    Pleasanton
    Posts
    4
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: why are using volatile in java

    Java is the best programing language. Java has a very bright scope in the programming field. There are many software companies on the market, which use this language for making software and games as well.

  8. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: why are using volatile in java

    Quote Originally Posted by ericlewis107 View Post
    Java is the best programing language. Java has a very bright scope in the programming field. There are many software companies on the market, which use this language for making software and games as well.
    I fail to see a purpose to your post - what does this at all have to do with the keyword volatile (a question asked over half a year ago)?

Similar Threads

  1. volatile
    By sivaprakash in forum Java Theory & Questions
    Replies: 0
    Last Post: March 13th, 2010, 12:30 AM