?
Solved

Where to use the modifier "volatile" in Java?

Posted on 2003-02-19
7
Medium Priority
?
301 Views
Last Modified: 2010-03-31
Hello,

I'm wondering about the modifier "volatile" in Java.

Is there any general approach when to use it and when not?
Which errors can occur when it is used without necessity?
Which errors can occur when it is missing?
0
Comment
Question by:Olaf_Bloemer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 7981485
> Is there any general approach when to use it and when not?

  Use it when you need to have variables holding data that will be shared between threads. You can also synchronize these variables.

> Which errors can occur when it is used without necessity?

  No errors, your application will just be slower.

> Which errors can occur when it is missing?

  Data could behave not in the order/manner you expect.
0
 
LVL 35

Expert Comment

by:girionis
ID: 7981497
0
 

Author Comment

by:Olaf_Bloemer
ID: 7982452
Thank you so far.

Is it correct to say, that using "volatile" is a weaker kind of protection for shared data than using "synchronized"?

I've read something about special problems with the 64 bit-types "long" and "double" which was something like "Variables of these types are read from memory in two 32-bit steps, and not using "volatile" can lead to inpredictable behaviour if a second thread steps in after the first one has read the first 32 bit."
This seems plausible, but does it mean that there is no problem with all other types with <=32 bit?

Thank you again!  :o)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Accepted Solution

by:
girionis earned 200 total points
ID: 7982665
> Is it correct to say, that using "volatile" is a weaker kind of protection for shared data than using "synchronized"?

  Hmm... not sure if I would say that... for beginning you can only declare variables as volatile while you can synchronize both variables and blocks of code.

  You can use volatile to prevent the compiler from moving/removing read/writes to fields since by using volatile the compiler will store into *memory* (and not registers) on assignment and retrieve from *memory* on access. It is a very important distinction since java compilers are allowed to cache shared fields in registers as well. This can cause concurrency problems since a thread could change its cached data of shared fields on a register and not in memory (and thus another thread could not see the change on the shared fields). However all shared fields in memory *are forced* to be thread-safe with their copies each thread holds.

  As an overall I use the volatile keyword for fields that depend on other threads to modify their value.

> I've read something about special problems with the 64 bit-types "long" and "double"

  Never heard of that before.. It would be nice if you could post a URL.

> This seems plausible, but does it mean that there is no problem with all other types with <=32 bit?

  Well only 32-bit long and double (from base types - objects by default are non-atomic) may not be atomic (the rest of the primitive types are all atomic). But if you declare a 32-bit long or double volatile then it is guaranteed to be atomic.
0
 

Author Comment

by:Olaf_Bloemer
ID: 7985952
Thank you girionis, for your answers.

Here I give you the URL for the 32bit/64bit discussion.
The section I meant is 17.4.

http://java.sun.com/docs/books/jls/second_edition/html/memory.doc.html

Best regards,

Olaf
0
 

Author Comment

by:Olaf_Bloemer
ID: 7985957
Thank you girionis, for your answers.

Here I give you the URL for the 32bit/64bit discussion.
The section I meant is 17.4.

http://java.sun.com/docs/books/jls/second_edition/html/memory.doc.html

Best regards,

Olaf
0
 
LVL 35

Expert Comment

by:girionis
ID: 7986104
 Ahhh... I see.. yes it's clear now, treating 64-bit long and double as two 32-bits can cause concurrency problems if they are not declared volatile. But at the end of the day it all boils down to the same thing: 32-bit long and double are not guaranteed to be atomic if they are not declared volatile. This is the case with 32-bit processors as well since from the base types only long and double may not be atomic.

  I think if we always remember that then we won't have any problems with long and double types in a multithreadng environment.

p.s. thank you for the points :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question