Is Java ArrayList / String / atomic variable reading thread safe?

前端 未结 6 1932
南方客
南方客 2021-02-14 00:27

I\'ve been mulling this over & reading but can find an absolute authoritative answer.

I have several deep data structures made up of objects containing ArrayLists, S

6条回答
  •  粉色の甜心
    2021-02-14 00:52

    If the data is never modified after it's created, then you should be fine and reads will be thread safe.

    To be on the safe side, you could make all of the data members "final" and make all of the accessing functions reentrant where possible; this ensures thread safety and can help keep your code thread safe if you change it in the future.

    In general, making as many members "final" as possible helps reduce the introduction of bugs, so many people advocate this as a Java best practice.

提交回复
热议问题