V
V
Valeriy_Morozov2021-01-29 19:10:00
Java
Valeriy_Morozov, 2021-01-29 19:10:00

Java, should reading collections or variables be Thread Safe?

I heard about Collections.synchronizedList, ConcurrentHashMap, CopyOnWriteArrayList, etc.
But I have a question, why can't I read a normal collection (without changing it) from different Thread. Or the same variable if it is final.
I would be very happy and grateful if someone could explain clearly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-01-29
@Valeriy_Morozov

why can't I read a regular collection (without modifying it) from different Threads

You can if no one changes it. Otherwise, you may not see any changes.
Or the same variable if it is final

The more you can.
I would be very happy and grateful if someone could explain clearly.

The topic is complex, do not look for answers on the Internet, here they can deceive. Better read " Java Concurrency in Practice ".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question