Home > Backend Development > C#.Net Tutorial > Example sharing on multi-threaded object creation in c#

Example sharing on multi-threaded object creation in c#

黄舟
Release: 2017-07-27 16:16:34
Original
1631 people have browsed it

The title of this article is about the problems I encountered when writing a blog in singleton mode, so I wrote a demo today to help me remember how to use multi-threading simply.

What I have been struggling with is how to instantiate the object multiple times in the for loop, so as to reproduce the error of multiple instance objects in the singleton mode without locking.

Let me first show you the multi-threaded instance object that I simply implemented.

Option 1:

Demo.cs


    public class Demo
    {        private static Demo _demo = null;        /// <summary>
        /// 构造函数        /// </summary>
        private Demo()
        {
            Console.WriteLine("构造了{0}", GetType().Name);
        }        /// <summary>
        /// 获取该类的唯一实例        /// </summary>
        /// <returns>该类的唯一实例</returns>
        public static Demo GetInstance()
        {            if (_demo == null)
                _demo = new Demo();            return _demo;
        }
    }
Copy after login

Program.cs, client code


                Demo d1 = null;
                Demo d2 = null;                //多线程创建对象实例
                var t1 = new Thread(() => { d1 = Demo.GetInstance(); });                
                var t2 = new Thread(() => { d2 = Demo.GetInstance(); });

                t1.Start();
                t2.Start();
                Thread.Sleep(1000);//主线程等待子线程执行完成,为d1和d2变量赋值
                Console.WriteLine("d1 == d2 {0}", object.ReferenceEquals(d1, d2));
                Console.Read();
Copy after login

Output:

Outputting two objects with different references achieves what I want.

But in my mind, there has always been a way for the for loop to create instances in multiple threads, but I just can’t remember it. I accidentally saw this when I was checking information today. I will write down the method immediately, and then work overtime at night to write it out, so that I can have an impression in my mind.

Option 2:

Program.cs


                for (int i = 0; i < 2; i++)
                {                    
                new Action(() => { Demo.GetInstance(); }).BeginInvoke(null, null);
                }
                Console.Read();
Copy after login

Output:

In this way, when debugging singleton mode, you can reproduce the unadded The lock was wrong, and it also solved my doubts. I found a way to solve the problem of multi-threaded instance creation in a for loop.

The above is the detailed content of Example sharing on multi-threaded object creation in c#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template