• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Volatile Confusion

 
Sagar Jambhulkar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this mean that we can expect no question on volatile in SCJP 1.4 exam??
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..

 
Edwin Keeton
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic