Whether a static method is used in multi-threading is a thread-safe issue

Source: Internet
Author: User

The members of a class are divided into two classes, static members (static member), and instance members (instance member). A static member belongs to a class and an instance member is an instance of an object, that is, a class.

A brief discussion of whether static fields and static methods are used in a class is a thread-safe issue.

We are aware that the call to static field and static method is called through the class. Static methods do not operate on specific instances, only static members. Instance methods can operate on specific instances, access static members, and access instance members.

So, is the static method used in multi-threading a thread-safe issue? This depends on whether static methods are used in static methods to see if static members are employed.

Because, when using the same static method in multi-threading, each thread uses a copy of its own instance field (instance field) and shares a static field. So, if the static method does not manipulate a static member, only the instance fields (instance field) are used inside the method, which does not cause security problems. However, if the static method operates on a static field, it needs to be handled securely in a static method with mutually exclusive access.

For a simple example, we use the Console.WriteLine (); WriteLine () is a static method of the Console.WriteLine class.

For ASP. NET, multiple client Access server side, this is a multithreaded example. As long as we understand the reason, we can safely use static methods in the data access layer in the three-tier architecture to access the database.

Look at a class first:

 1  public  class   Test  2  { 3  public  static   string hello (string str)  4   {   5  String tmp =   6  tmp = tmp + str;  7  return   TMP;  8  }  9  }

Will the Hello method have multiple thread safety issues? No!

Static methods There is no thread-safety issue if static variables are not used.
Why is it? Because of variables declared within a static method, each thread is called to create a new copy without sharing a single storage unit. For example, the TMP here, each thread will create its own copy, so there is no wired security issues.
Note: Static variables, because they occupy a store during class loading, each thread is shared with this store, so if static variables are used in static methods, this will be a wired security issue!

Whether a static method is used in multi-threading is a thread-safe issue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.