| Author |
Volatile Confusion
|
Sagar Jambhulkar
Greenhorn
Joined: May 19, 2005
Posts: 7
|
|
Can anyone please explain with example the usage of volatile feilds? Please give code where if we do not use volatile, problem is occuring.I am totally confused abt the caching concept of varaibles by threads and hence the need for volatile feilds. Thx in advance.
|
 |
Timmy Marks
Ranch Hand
Joined: Dec 01, 2003
Posts: 226
|
|
The volatile keyword is much more important in C than in Java. I have never used it in Java, but it is used in C for a couple of purposes: 1. when programming peripherals whose register values can change, you should declare variables volatile so the compiler knows to always fetch the values from the peripheral's registers instead of using local copies 2. when programming using global variables that can be changed outside the current thread, volatile needs to be used to keep the compiler from optimizing your intent out of the program.
|
 |
Sagar Jambhulkar
Greenhorn
Joined: May 19, 2005
Posts: 7
|
|
|
Does this mean that we can expect no question on volatile in SCJP 1.4 exam??
|
 |
Timmy Marks
Ranch Hand
Joined: Dec 01, 2003
Posts: 226
|
|
It has been so long since I took the exam, I couldn't really tell you. There is a list of concepts which you are required to be familiar with for the exam, but which ones in particular will be on your exam? Only fate will decide. If it is listed, learn it. I can't tell you any more than that.
|
 |
amit taneja
Ranch Hand
Joined: Mar 14, 2003
Posts: 806
|
|
you will expect the question on volatile.. like you have to choose the keyword from names given in option... so you have to choose volatile as a keyword from the given string of idtefiers and keywords.. so ..do know atleast that voloatile is a keyword..
|
Thanks and Regards,<br />Amit Taneja
|
 |
Edwin Keeton
Ranch Hand
Joined: Jul 10, 2002
Posts: 214
|
|
What you need to know about 'volatile' for the exam is a) that it's a keyword, b) what it can modify, c) illegal combinations with other modifiers, and d) its general meaning or intent. If you know that separate threads will read and write a volatile member variable from a shared memory area (rather than a thread local copy), that volatile only applies to member variables ("data fields"), and that a member variable cannot be volatile and final, you'll have it covered for the exam.
|
SCJP, SCWCD
|
 |
amit taneja
Ranch Hand
Joined: Mar 14, 2003
Posts: 806
|
|
If you know that separate threads will read and write a volatile member variable from a shared memory area (rather than a thread local copy), what does that means ??
|
 |
Edwin Keeton
Ranch Hand
Joined: Jul 10, 2002
Posts: 214
|
|
It means that separate threads may make their own copies of variables and work with those copies in the execution of the thread. Then another thread may come along and make its own copy of this variable which is now out of sync with its value in the first thread. The volatile keyword tells the compiler to make sure that all threads use the same copy of the variable. The reason threads may make their own copies of variables is due to optimization. (It's sort of like caching.) Switching thread context just to retrieve a value is a relatively expensive operation. The actual implementation of how this is done depends on the JVM.
|
 |
amit taneja
Ranch Hand
Joined: Mar 14, 2003
Posts: 806
|
|
thanx for ur reply.... i understand to the point that every thread has its own copy of variable...and will work with it but what i don't understand is that when you say The reason threads may make their own copies of variables is due to optimization. (It's sort of like caching.) Switching thread context just to retrieve a value is a relatively expensive operation. what does that means... if anybody can explain ...
|
 |
Edwin Keeton
Ranch Hand
Joined: Jul 10, 2002
Posts: 214
|
|
It's way more than you need to know for the exam. All it means is that an implementation of the JVM might copy non-volatile member variables in an area of memory that only a single thread can access. Other threads might also make such copies. Since there are different copies of the same variable, a change made to one copy will not be reflected in the other. When the variable is marked volatile, the compiler makes sure that each separate thread reads the same variable, not separate copies.
|
 |
 |
|
|
subject: Volatile Confusion
|
|
|