Results 1 to 14 of 14
  1. #1
    peiceonly is offline Member
    Join Date
    Mar 2007
    Posts
    41
    Rep Power
    0

    Default Keyword : volatile

    What is this used for ?
    Why this is not used for local variables

  2. #2
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    The volatile keyword is used on variables that may be modified simultaneously by other threads. This warns the compiler to fetch them fresh each time, rather than caching them in registers. This also inhibits certain optimisations that assume no other thread will change the values unexpectedly. Since other threads cannot see local variables, there is never any need to mark local variables volatile.

  3. #3
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    I haven't quite get the matter, could u give some examples bout the subject goldhouse?
    Or maybe a link about it, kinda hard to understand it sometimes without an example

    Thanks :)
    CruxBlack

  4. #4
    jdev is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default

    goldhouse example is a good one. I'll give you a different example.

    Volatile comes from the C language (not sure C was the first to use it, though!). If you have a hardware register which represents a temperature value, for example, if you don't tell the compiler that that address is volatile the compiler might use an internal register to store that value, which would be wrong because the value keeps changing (in the external device) while the value in internal register won't change. In this case volatile means "get always the value from this address".
    Hope this helps

  5. #5
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    thanks jdev

  6. #6
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    Err...i havent dig too much into threading and concurrency actually, have been reading about it from some books, but i still haven't understand much of its concept :(
    All i can think when u say
    Quote Originally Posted by jdev
    "get always the value from this address"
    was, doesn't the static keyword do the same thing? Though, i realize that may not be the case :(
    Ill start a thread in the New to Java section since my questions are gonna be rather basic bout threading and unfit in the Advanced Java, but thanks all

    CruxBlack

  7. #7
    vibhor.ec is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default

    u cant' serialize volatile variables.i.e. cant save the volatile varible of an object

  8. #8
    kishoreakkala is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default difference between volatile transient and static variables

    tell me main difference between volatile transient and static variables

  9. #9
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by cruxblack View Post
    was, doesn't the static keyword do the same thing? [/b]
    Not at all. Static means that there is one set of variables per class. Or more carefully, one set per instance of the class as loaded by a single class loader.

    Static variables in Java are the same as "class side" variables in Smalltalk.

    Volatile is all about telling the compiler that the value it thinks is in a variable may be changed elsewhere.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by kishoreakkala View Post
    tell me main difference between volatile transient and static variables
    Let me instead tell you a similarity: they can all be looked up on Google by an enterprising self-motivated student.

  11. #11
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default Weak synchronization / visibility

    Marking a variable volatile provides a "weak" form of synchronization. Conceptually, it's more or less as though each single read/write to the variable sits inside a synchronized block synchronized on that variable. (Pre Java 5, this actually wasn't quite true but people introduced bugs by thinking it was; in Java 5, it's more or less true.) So as a first approximation, the following:
    Java Code:
    volatile int x;
    ...
    x = 5;
    x++;
    can be thought of more or less as being expanded to the following (note you couldn't actually write it like this because you can't synchronize on a primitive):
    Java Code:
    int x;
    ...
    synchronized (x) { x = 5; }
    int t;
    synchronized (x) { t = x; }
    t++;
    synchronized (x) { x = t; }
    A bit more strictly:
    - a write to a volatile variable is always visible to another thread that reads that variable
    - (as of Java 5), accesses to volatile variables are not re-ordered with accesses to other variables
    - (as of Java 5), the language provides provides facilities for accessing volatile variables via atomic get-set instructions
    - (as of Java 5), reading/writing a volatile variable provides similar guarantees of visibility of other variables as entering/exiting a synchronized block.

    There are actually some differences between volatile and synchronized (some subtle, some less subtle):
    - generally, accessing a volatile is always a single-read or single-write, so it will never block (whereas a thread could block waiting to obtain a synchronized lock)
    - ordinarily, volatile doesn't give atomicity: e.g., you can't "lock" a read and a write together (as in x++), as you could by putting both accesses inside the same synchronized block; but as mentioned, via the Java 5 atomic package, you can actually get atomic (non-blocking) get-set operations on a volatile variable or element of a volatile array
    - you can't synchronize on a null object reference or on a primitive (but a volatile variable can be null or a primitive);
    - a read/write to a volatile variable generally has a lower performance impact than synchronized (on Intel platforms, the performance is essentially just the fact that volatile blocks certain compiler optimisations)

    So when is volatile used? Generally:
    - where a variable is accessed by multiple threads, and
    - the value to write to the variable doesn't depend on the previously-read value (so not things like x++)
    The most typical case is a "simple flag" that, say, one thread will poll to find out when to stop, and another thread will set when it wants it to stop.

    Under the hood, volatile variables are a significant language feature, that essentially make the entire Java 5 concurrency framework possible. But most programmers will probably want to use these library classes rather than "raw" volatile variables.

    Related articles I've written:

  12. #12
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Neil knows a lot more about threading than I do, but I will add a couple of observations anyway.

    Threads can, but don't always, make their own copies of variables. If a copy is made, two threads can have different values for the field until synchronize forces them to be updated. volatile stops this behavior.

    volatile adds overhead, but not as much as synchronize.

    In general, don't share fields across threads unless there is a specific purpose in doing so. When you do, choose a method of controlling those fields carefully.

  13. #13
    Nikhilkumr is offline Member
    Join Date
    Oct 2010
    Location
    Nagpur
    Posts
    6
    Rep Power
    0

    Default

    I am new in java group. so for my mistake sorry.
    Last edited by Nikhilkumr; 11-17-2010 at 01:00 PM.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default

    Evidently this thread is anything but transient.

    Nikhilkumr, are you dyslexic in reading dates?

    db

Similar Threads

  1. finalize keyword
    By bugger in forum New To Java
    Replies: 6
    Last Post: 10-01-2008, 12:28 PM
  2. volatile and synchornized
    By goldhouse in forum Threads and Synchronization
    Replies: 3
    Last Post: 04-23-2008, 08:38 PM
  3. transient keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 10:27 AM
  4. volatile variable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 10:26 AM
  5. Use of this keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •