多线程18-QueueUserWorkItem

    class Proigram
    {
        private static void AsyncOperation(object state)
        {
            Console.WriteLine("Operation state={0}", state);
            Console.WriteLine("work thread id={0}", Thread.CurrentThread.ManagedThreadId);
            Thread.Sleep(TimeSpan.FromSeconds(2));
        }
        static void Main()
        {
            const int x = 1;
            const int y = 2;
            const string lambdaState = "lambda state 2";
            ThreadPool.QueueUserWorkItem(AsyncOperation);
            Thread.Sleep(TimeSpan.FromSeconds(1));

            ThreadPool.QueueUserWorkItem(AsyncOperation,"saync state");
            Thread.Sleep(TimeSpan.FromSeconds(1));

            ThreadPool.QueueUserWorkItem(state => {
                Console.WriteLine("Operation state :{0}", state);
                Console.WriteLine("ThreadId={0}", Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(5);
            },"lambda state");

            ThreadPool.QueueUserWorkItem(_ => {
                Console.WriteLine("operation state:{0},{1}", x + y, lambdaState);
                Console.WriteLine("worker thread id :{0}", Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(4);
            },"lambda state");

            Thread.Sleep(TimeSpan.FromSeconds(2));
        }
    }
posted @ 2016-06-23 10:39  shidengyun  阅读(275)  评论(0编辑  收藏  举报