ThreadLocal

来源:互联网 发布:装饰设计软件 编辑:程序博客网 时间:2024/05/09 20:06
        if(t == null)                t = new ThreadLocal<Random>(() => new Random());

声明一个 Random 类的对象,作为 Threadlocal

        private void sendComment()        {            try            {                Comment2ForAE.comment2forae c2fae = new Comment2ForAE.comment2forae();                DateTime start = DateTime.Now;                String userid = t.Value.Next(10000).ToString();                Boolean isOK = c2fae.addCommentToAEcomment2(userid, "C"/*C means common*/, "wahahaha" + userid);                DateTime end = DateTime.Now;                TimeSpan duration = end.Subtract(start);                String output;                if (isOK == true)                    output = userid + ":" + duration.TotalMilliseconds.ToString() + "ms<br/>";                else                    output = " oh shit. <br/>";                                //output += userid + ":" + duration.TotalMilliseconds.ToString() + "<br/>";                Session["output"] += output;            }            catch (Exception ex)            {                Session["output"] += ex.ToString() + "<br/>";            }                             }

然后使用就好了


原创粉丝点击