Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use singleton mode and multithreading in C #

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use singleton mode and multithreading in C#. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Singleton model

Let's first look at two sample code for creating a singleton pattern.

1. Hungry Han style

To create a singleton mode in hungry Chinese style, an object instance is initialized directly in the program:

Class Good {/ private static variable, initialize / private static Good Instance = new Good () directly / Private constructor / private Good () {} / get the static method of static instance / public static Good GetInstance () {return Instance;}} 2, lazy

The above use of hungry Han style to create a singleton pattern has a disadvantage: if the program is not used, it will also create an instance, which will also take up some memory. Sometimes you need to create an instance only when you really need to use it for the first time, so you need to use lazy style to create a singleton pattern.

Class Good {/ private static variable / private static Good Instance = null / Private constructor / private Good () {} / get the static method of the static instance / public static Good GetInstance () {if (Instance==null) {Instance= new Good ();} return Instance Second, singleton mode and multithreading

The above two methods of creating singleton pattern have no problem when using single thread, the singleton pattern created by hungry Han style has no problem when using multi-thread, and the singleton pattern created by lazy mode has a problem under multi-thread. So how to solve it?

You can add a [MethodImpl (MethodImplOptions.Synchronized)] annotation to the GetInstance method as a synchronization method. You can also use the lock keyword. Let's take a look at how to use the lock keyword:

Class Good {/ private static variable / private static Good Instance = null; private static object locker = new object () / Private constructor / private Good () {} / get the static method of the static instance / public static Good GetInstance () {/ / use lock Lock (locker) {if (Instance = = null) {Instance = new Good () } return Instance;}

The singleton can be guaranteed in a multithreaded environment by using the lock keyword. However, it is still a problem to modify the code in this way. in fact, it only makes sense when the lock is added when Instance is null. Later calls, each thread has to lock the locker, which will cause performance degradation. You can use double checking (double-check) to resolve performance issues. We make the following modifications to the above code

Class Good {/ private static variable / private static Good Instance = null; private static object locker = new object () / the private constructor / private Good () {} / get the static method of the static instance / public static Good GetInstance () {/ / first check whether the Instance variable is null if (Instance = = null) {/ / Use lock lock (locker) {if (Instance = = null) {Instance = new Good () } return Instance;}}

In this way, the lock will be added only when it is initialized for the first time. Later, when it is accessed, the Instance variable is no longer null, and the Instance variable is returned directly.

Thank you for reading! This is the end of this article on "how to use singleton mode and multithreading in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report