H
H
have_a_questions2017-01-04 13:55:39
Java
have_a_questions, 2017-01-04 13:55:39

What is the difference between a volotile ThreadLocal and a static variable?

Good day. Please tell me how volotile ThreadLocal and static variable differ from each other and when to use them?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Boyarshinov, 2017-01-04
@have_a_questions

volatile allows different threads to see the "actual" value of a variable, because any changes are immediately written to memory, while if you do not declare a variable with this keyword, its value can be stored in the cache.
A static variable is a variable that is initialized the first time the class is called and its value is the same for all instances of the class.
When to use one or the other?
Well, the first one, which is obvious, when working with primitive types from several threads (for example, 2 threads access the counter variable)
and static ... Well, there are many different options

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question