zoukankan      html  css  js  c++  java
  • CallContext

    1.线程本地存储区的专用集合对象,并提供对每个逻辑执行线程都唯一的数据槽。
    2.数据槽不在其他逻辑线程上的调用上下文之间共享。

        class Program
        {
            static Jason_TestEntities Current
            {
                get
                {
                    Jason_TestEntities dbContext = CallContext.LogicalGetData("CurrentContext") as Jason_TestEntities;
                    if (dbContext == null)
                    {
                        Console.WriteLine("重新获取DBContext" + Thread.CurrentThread.ManagedThreadId);
                        dbContext = new Jason_TestEntities();
                        dbContext.Configuration.ValidateOnSaveEnabled = false;
                        CallContext.LogicalSetData("CurrentContext", dbContext);
                    }
                    return dbContext;
                }
            }
    
            static void Main(string[] args)
            {
                for (int i = 0; i < 5; i++)
                {
                    ThreadStart threadStart = new ThreadStart(select);
                    Thread thread = new Thread(threadStart);
                    thread.Start();
                }
                Console.WriteLine("主线程结束");
                Console.Read();
            }
    
            static void select()
            {
                Console.WriteLine("线程开始" + Thread.CurrentThread.ManagedThreadId);
                Current.wolf_example.Add(new wolf_example()
                {
                    Name = "name" + Current.wolf_example.Count(),
                    Money = Current.wolf_example.Count(),
                });
                Current.SaveChanges();
                Console.WriteLine("线程结束" + Thread.CurrentThread.ManagedThreadId);
            }
        }

  • 相关阅读:
    VIJOS-P1340 拯救ice-cream(广搜+优先级队列)
    uva 11754 Code Feat
    uva11426 GCD Extreme(II)
    uvalive 4119 Always an Interger
    POJ 1442 Black Box 优先队列
    2014上海网络赛 HDU 5053 the Sum of Cube
    uvalive 4795 Paperweight
    uvalive 4589 Asteroids
    uvalive 4973 Ardenia
    DP——数字游戏
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/7761899.html
Copyright © 2011-2022 走看看