首页 > 代码库 > 多线程 中 对多次初始化问题解决方案
多线程 中 对多次初始化问题解决方案
今天在看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(当前时间)肯定是一个值,呵呵。
多线程 中 对多次初始化问题解决方案
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。