码迷,mamicode.com
首页 > 编程语言 > 详细

多线程 中 对多次初始化问题解决方案

时间:2014-12-05 00:51:15      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

今天在看MSDN 库源代码时发现了一个类

 LazyInitializer.EnsureInitialized
 并行计算时用到的.

MSdn代码

// Used to hold any exceptions encountered during action processing
                    ConcurrentQueue<Exception> exceptionQ = null; // will be lazily initialized if necessary
 
                    // This is more efficient for a large number of actions, or for enforcing MaxDegreeOfParallelism.
                    try
                    {
                        // Launch a self-replicating task to handle the execution of all actions.
                        // The use of a self-replicating task allows us to use as many cores
                        // as are available, and no more.  The exception to this rule is
                        // that, in the case of a blocked action, the ThreadPool may inject
                        // extra threads, which means extra tasks can run.
                        int actionIndex = 0;
                        ParallelForReplicatingTask rootTask = new ParallelForReplicatingTask(parallelOptions, delegate
                        {
                            // Each for-task will pull an action at a time from the list
                            int myIndex = Interlocked.Increment(ref actionIndex); // = index to use + 1
                            while (myIndex <= actionsCopy.Length)
                            {
                                // Catch and store any exceptions.  If we don't catch them, the self-replicating
                                // task will exit, and that may cause other SR-tasks to exit.
                                // And (absent cancellation) we want all actions to execute.
                                try
                                {
                                    actionsCopy[myIndex - 1]();
                                }
                                catch (Exception e)
                                {
                                   <span style="color:#ff0000;"> LazyInitializer.EnsureInitialized<ConcurrentQueue<Exception>>(ref exceptionQ, () => { return new ConcurrentQueue<Exception>(); });
                                    exceptionQ.Enqueue(e);</span>
                                }
 
                                // Check for cancellation.  If it is encountered, then exit the delegate.
                                if (parallelOptions.CancellationToken.IsCancellationRequested)
                                    throw new OperationCanceledException(parallelOptions.CancellationToken);
 
                                // You're still in the game.  Grab your next action index.
                                myIndex = Interlocked.Increment(ref actionIndex);
                            }
                        }, TaskCreationOptions.None, InternalTaskOptions.SelfReplicating);
 
                        rootTask.RunSynchronously(parallelOptions.EffectiveTaskScheduler);
                        rootTask.Wait();
                    }
                    catch (Exception e)
                    {
                        LazyInitializer.EnsureInitialized<ConcurrentQueue<Exception>>(ref exceptionQ, () => { return new ConcurrentQueue<Exception>(); });
 
                        // Since we're consuming all action exceptions, there are very few reasons that
                        // we would see an exception here.  Two that come to mind:
                        //   (1) An OCE thrown by one or more actions (AggregateException thrown)
                        //   (2) An exception thrown from the ParallelForReplicatingTask constructor
                        //       (regular exception thrown).
                        // We'll need to cover them both.
                        AggregateException ae = e as AggregateException;
                        if (ae != null)
                        {
                            // Strip off outer container of an AggregateException, because downstream
                            // logic needs OCEs to be at the top level.
                            foreach (Exception exc in ae.InnerExceptions) exceptionQ.Enqueue(exc);
                        }
                        else
                        {
                            exceptionQ.Enqueue(e);
                        }
                    }
 
                    // If we have encountered any exceptions, then throw.
                    if ((exceptionQ != null) && (exceptionQ.Count > 0))
                    {
                        ThrowIfReducableToSingleOCE(exceptionQ, parallelOptions.CancellationToken);
                        throw new AggregateException(exceptionQ);
                    }


下以内容为转载内容以方便更好理解上面的内容.

LazyInitializer.EnsureInitialized是frameworks4.0引入的新东西,实现对属性延时初始化的功能,它作用在System.Threading命名空间下,所以,它与多线程有着密切的关系,即当多人同步使用这个方法时,对存储的对象有着某种作用,这是msdn的相关说明:

这个方法可以用于多个执行者初始化Target目录对象。

在多个执行者同时存取这个方法的情况下,可能会建立多个T执行个体,但只有一个执行个体会存储至target。在些类情况下,这个方法将不会放置未储存的对象。如果这类对象必须被放置,则由呼叫端判断未使的对象,然后再对物件进行适当的放置。

对于概念不清楚的同步,没有关系,看下面的例子如完全明白了,呵呵

下面的实例介绍了对属性的两个初始化,并进行比较,延时初始化的好处,即在对象使用时再去初始化它当一个方法体中,如果一个对象初始化了一次,不要再进行重复的初始化工作。

代码1,展现了性能不好的情况

代码2,展现了优化的情况

代码3,微软为了我们进行了封装,在多线程中表现更好

代码1

class TestLazyInitializer1
    {

        public People People
        {
            get
            {

                return new People();

            }
        }

        public void Print1()
        {
            Console.WriteLine(People.Name);
        }
        public void Print2()
        {
            Console.WriteLine(People.Name);
        }

    }
代码2:

class TestLazyInitializer2
    {
        People _people;
        public People People
        {
            get
            {

                return _people == null
                    ? (_people = new People())
                    : _people;

            }
        }

        public void Print1()
        {
            Console.WriteLine(People.Name);
        }
        public void Print2()
        {
            Console.WriteLine(People.Name);
        }

    }
代码3

class TestLazyInitializer
    {
        private People _people;
        /// <summary>
        /// 延时初始化指定属性
        /// </summary>
        public People People
        {
            get
            {
                return LazyInitializer.EnsureInitialized(
                    ref _people, () =>
                    {
                        return new People();
                    });
            }
        }

        public void Print1()
        {
            Console.WriteLine(People.Name);
        }
        public void Print2()
        {
            Console.WriteLine(People.Name);
        }

    }

而它们运行的结果,我们可想而知了,当一个类中多次使用同一个对象时,性能不好的,返回的Name(当前时间),肯定是不同的,而性能好的,只初始化一次的,返回的Name(当前时间)肯定是一个值,呵呵。




bubuko.com,布布扣



















 

多线程 中 对多次初始化问题解决方案

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://blog.csdn.net/daonidedie/article/details/41733157

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!