Multiple threads reading a variable

I'm reading "Effective Java Second Edition" by Joshua Bloch and im confused by below statement with regard to concurrency -

"The language specification gaurantees that reading or writing a variable is atomic unless the variable is of type long or double[JLS, 14.4.7]. In other words, reading a variable other than a long or double is gauranteed to return a value that was stored into that variable by some thread, even if multiple threads modify the variable concurrently and without synchronization."

This is stated on last paragraph of page 259 in case anyone has book on hand.

Will the variable referred to not always have a value even if multiple threads are modifying it ?

Answers


You have to read the sentence as a whole. Here, let me reword it for you:

"In other words, reading a variable other than a long or double is gauranteed to return a value that was stored into that variable by some thread, even if multiple threads modify the variable concurrently and without synchronization"

becomes:

"Let's assume the variable isn't a long or a double (because then special rules apply). Even if there are multiple threads modifying the variable at the same time without synchronization to protect it, then a variable will always have a value.

This value will always be one of the values that one of the threads wrote. You won't be able to tell which one ahead of time, but it will always be a value that one of them wrote. It won't ever be a corrupt half-and-half value."


Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.