static variables in multithreading

后端 未结 4 771
耶瑟儿~
耶瑟儿~ 2021-01-02 07:04

I found that declaring a variable as static makes no sense in Multi-Threading. I assume that, this is because of every thread has its own

4条回答
  •  天涯浪人
    2021-01-02 07:11

    static makes no sense in Multi-Threading.

    Im afraid you are making the reverse statement. Static variable is a shared resource, which can be used to exchange some information among different threads. And we need to be careful while accessing such a shared resource. Hence, we need to make sure that the access to static variables in multi-threaded environment is synchronized.

    every thread has its own stack

    This is a correct statement. Each thread has its own stack but they share the process heap. Stack holds only the local variables and not the variables on the heap. Static variables are stored in the PermGen section of the heap and hence the access to them should be well guarded.

提交回复
热议问题