Will static methods over multithreading cause thread-safe problem?

  Most of the time when we are using multi-threading we are likely facing the thread-safe issue,it seems that this problem always come up with calling static methods or functions in a thread when multi-threading coding.But is it correct? For more detail, i've done a lot google to search this issue in detail.

  After reading some helpful blogs and tech articles,i find out that static method or function is not the key or the cause of the thread-safe problem.In fact what i thought about thread-safe problem is even wrong. The most important thing to this issue is whether one or more static variables are being used in any single thread in multi-thread coding because only static resources and global variables are shared by threads.If no static variables or static fields are occupied in static methods or functions in these thread,we don't need to worry about the synchro of these static variables or static fields.Be clear that instance variables and fields(including properties of course)  aren't shared by different thread,they only belong to the thread which called the static method or function declaring them.

  So,let's make it more brief:

    1.If no static resources or global variables are used in a static method/function, we are not facing the thread-saft problem,even if we are using multi-thread technology.

    2.If any instance variable is declared in a static method/function,this variable only belongs to the thread which call this static method/function.

    See also:

      http://blog.csdn.net/scucj/article/details/1394523

 

 

posted on 2013-01-23 00:15  wyman25  阅读(267)  评论(0编辑  收藏  举报

导航