Are Java collections safe if they are read but not modified in multiple threads?

Is it possible to use the standard Collections classes (as opposed to the simultaneous ones) if I guarantee that the code does not modify the data in multiple threads. The code I'm talking about is completely under my control, and I don't mutate it after the initial (single-threaded) phase of the population.

I know that some classes, such as DateFormat, are not thread safe because they store intermediate states as they are used. Are collections (ArrayList, Tree Map, etc.) safe though?

+4
source share
4 answers

, , , . , , ( Collections.unmodifiableList) ( , , ).

+8

. API Java , , , TreeMap:

, . , , . (A - , ; , .)

. , , .

+1

, ( ) .

, , , , .

0

, :

  • Multiple threads (as you wrote).
  • The same thread in the current opening of Iterration.

If you check these two, you can use standard collections.

Sincerely.

0
source

Source: https://habr.com/ru/post/1540490/


All Articles