When is volatile necessary
If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants. Follow BitterCoffey. Editorial page content written by Neil Coffey. All rights reserved. Toggle navigation Javamex: Java Tutorials. Math class Methods of the java. A variable should be declared volatile whenever its value could change unexpectedly.
In practice, only three types of variables could change:. Embedded systems contain real hardware, usually with sophisticated peripherals. These peripherals contain registers whose values may change asynchronously to the program flow.
As a very simple example, consider an 8-bit status register that is memory mapped at address 0x It is required that you poll the status register until it becomes non-zero.
The naive and incorrect implementation is as follows:. This code will almost certainly fail as soon as you turn compiler optimization on.
That's because the compiler will generate assembly language here for an bit x86 processor that looks something like this:. The rationale of the optimizer is quite simple: having already read the variable's value into the accumulator on the second line of assembly , there is no need to reread it, since the value will duh! Thus, from the third line of assembly, we enter an infinite loop. To force the compiler to do what we want, we should modify the declaration to:.
Subtler sorts of bugs tend to arise when registers with special properties are accessed without volatile declarations. For instance, a lot of peripherals contain registers that are cleared simply by reading them. Extra or fewer reads than you are intending could result in quite unexpected behavior in these cases. Interrupt service routines often set variables that are tested in mainline code. For example, a serial port interrupt may test each received character to see if it is an ETX character presumably signifying the end of a message.
An incorrect implementation of this might be:. With compiler optimization turned off, this program might work. However, any half decent optimizer will "break" the program. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. The compiler, the runtime system, and even hardware may rearrange reads and writes to memory locations for performance reasons.
Fields that are declared volatile are excluded from certain kinds of optimizations. There is no guarantee of a single total ordering of volatile writes as seen from all threads of execution. For more information, see the Volatile class. On a multiprocessor system, a volatile read operation does not guarantee to obtain the latest value written to that memory location by any processor. Similarly, a volatile write operation does not guarantee that the value written would be immediately visible to other processors.
FutureTask implements the interface Future so calling the method FutureTask. Here is a potential program flow to explain this: Thread A set variable x and y of object OA to one and calls FutureTask. Now, Thread B reads this object calling FutureTask. To make the example more interesting, Thread A now sets variable x to two. If Thread B reads variable y, it surely sees value one, since the cache was synchronized between the call to FutureTask. But for variable y, Thread B reads one or two, depending on which cores the two Threads were running on.
If you forget to declare a field as volatile, a thread might read a stale value. But the chance to see this during tests is rather low. Since the read and the write must happen at almost the same time and on different cores to read a stale value, this happens only under heavy load and after a long run time, in production. The volatile field is needed to make sure that multiple threads always see the newest value, even when the cache system or compiler optimizations are at work.
Reading from a volatile variable always returns the latest written value from this variable. And since the processor synchronizes the complete cache, we see the latest written values of all variables. The methods of most classes in the java. Often by using volatile fields internally. See the original article here. Performance Zone. Thanks for visiting DZone today,. Edit Profile.
0コメント