Z
Z
Zulkund2016-02-26 11:59:10
Java
Zulkund, 2016-02-26 11:59:10

Explain the meaning of volatile identifier in Java?

Gentlemen, I can not understand the meaning of the volatile identifier, please explain in simple words. The following is not clear, if several threads use 1 object and the changed data is often stored in the cache, then wouldn't it be easier to assign the static modifier to the object? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sirs, 2016-02-26
@Zulkund

wouldn't it be easier to assign the static modifier to the object?

No, it's not easier. A static field belongs to the class itself (the object of the class), not to objects. How will you modify it?
volatile allows you to see changes to a variable in all threads at once, but do not forget that this is only true for atomic operations!
If you are looking for a specific solution to a specific problem - look towards AtomicInteger, AtomicLong, etc. if you need something similar to counters, towards ReentrantLock if you have some kind of shared resource, and towards ConcurrentHashMap if you have a collection that many threads should work with ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question