Should my field be mutable upon initialization in the background thread?

When the application starts, I create an instance of the class in the background thread, and then assign it to a variable. I later access this variable from my main thread. This variable is assigned only once.

My understanding is that I do not need to use the volatile keyword here because the link cannot be cached until it is first available in the user interface thread. Am I right in my thinking, or am I missing something?

+4
source share
1 answer

Am I right?

Yes. Caching is only a problem when one thread repeatedly reads a variable (written from another thread).

And since the destination of the link is atomic, you are safe.

+3
source

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


All Articles