Static variables and multithreading in java

后端 未结 7 1682
心在旅途
心在旅途 2020-12-02 13:31

Is a static member of a class present as only a single instance per process or thread? Meaning does each thread has its own copy of the static member variable of the class?

相关标签:
7条回答
  • 2020-12-02 14:30

    There is one copy of static variable per class-loader that loaded this class. This more-or-less means per-process, however you need to be aware of the difference.

    E.g. when two web-apps have the same class bundled, the class will be loaded twice, thus having two copies of the same static field.

    If you need a variable having independent value on a thread basis, have a look at ThreadLocal.

    0 讨论(0)
提交回复
热议问题