What is the volatile keyword in Java?

Asked 2 years ago, Updated 2 years ago, 36 views

I was looking at the Java document and saw the keyword volatile, what is this and what is it used for?

java volatile

2022-09-22 12:45

1 Answers

Volatile is the one who synchronizes in a multi-threading environment. No matter what thread changes the value during read write, it always allows you to read the latest value. Then, what is the difference from the synchronized keyword?

It is the same as volatile or synchronized that allows the same value to be read for the access of that variable during the operation.

However, volatile is synchronized only for visibility minus elements of the task, i.e. read and write actions. And it has atomic properties It's hard to say. For example...

int count = 10;

Assume that you have a variable called , which threads A and B use.

Almost at the same time, A thread reads and manipulates count. B also reads and manipulates the count. In this case, the value of count between the two threads does not point to the same value, the same memory address. Threads are manipulated by copying the original value to their own storage area.

So count may have different values for each thread. The value changed in A may not be read in B forever. After one process or another, threads in A and B return the manipulated value to the original value when the operation is finished, but the operation may not end or the batch optimization (described below ordering). Then the variable count would be inconsistent between threads.

Inconsistencies in reading variables can also occur as a result of reordering, a kind of optimization. Reordering usually occurs during compilation, and programmer-generated code is optimized to run faster when compiled. During such optimization, the order of reading and writing can change, which can lead to problems in multi-threaded environments where the order is important.

Variables that write volatile are excluded from reordering, and always read and write in the order specified by the programmer.

This is the same characteristic as the first problem. Usually, the write-read of a variable may seem atomic at first glance (let's understand it as a perfect unit of work with no room for intervention...), but when a variable is assigned to the actual count, it is not written completely within a single memory task.

Oh, data types like int are written at once...; But a larger type, long or object...And what happens when you assign and compute variables, you don't have complete atomicity, you have multiple memory operations. (Technically speaking, it may vary depending on the JVM implementation...)

long stat = 324L;

In the above code, long is the data type of 8 bytes, or 64 bits. When assigning variables here, Java is assigned in 32-bit increments. First 32-bit assignment, then 32-bit assignment... What if another thread reads the value when the first 32 bits are assigned? There will be an unusual situation (of course it's a very short moment)

If the stat variable is a volatile keyword variable, the assignment is atomic, so there is no problem.

It's no different than synchronized No. If you look at the code below

int val = stat + 10;

This code is dangerous for multi-threading even if you have a volatile declaration in the val. Obviously, depending on the access order of the threads, some threads will take the value of val plus 10, and others will read the value without 10. This is because the above code goes through three tasks: casting stat as int, adding 10 and assigning it back to val.

Syncronized atomizes the work itself. It's something that volatile can't do.

http://blog.javarouka.me/2012/04/volatile-keyword-in-java.html


2022-09-22 12:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.