Are non-synchronised static methods thread safe if they don't modify static class variables?

后端 未结 5 828
悲哀的现实
悲哀的现实 2020-11-28 17:20

I was wondering if you have a static method that is not synchronised, but does not modify any static variables is it thread-safe? What abou

5条回答
  •  生来不讨喜
    2020-11-28 18:22

    Use the static keyword with synchronized static methods to modify static data shared among threads. With the static keyword all the threads created will contend for a single version of the method.

    Use the volatile keyword along with synchronized instance methods will guarantee that each thread has its own copy of the shared data and no read/ writes will leak out between threads.

提交回复
热议问题