线程

    class Program
    {
        static void Main(string[] args)
        {
            //线程
            Thread t = new Thread((objectParam) => Console.WriteLine("线程执行方法,有无参数{0}", objectParam != null));
            t.IsBackground = true;
            object obj = new object();
            t.Start(obj);
            Console.WriteLine("执行线程");

            //线程池
            ThreadPool.QueueUserWorkItem((objectParam) => Console.WriteLine("线程方法加入匿名方法"));

            //并行循环Parallel
            int result = 0;
            int lockResult = 0;
            object lb = new object();
            //并行循环
            //并行循环用于一次执行多相同任务,或计算结果和循环的游标没有关系只和执行次数有关系的计算。
            Parallel.For(0, 10, (i) =>
            {
                result += 2;
                lock (lb)
                {
                    lockResult += 2;
                    Thread.Sleep(100);
                    Console.WriteLine("i={0},lockResult={1}", i, lockResult);
                }
                Console.WriteLine("i={0},result={1}", i, result);
            });

            //任务  一个可以有返回值(需要等待)的多线程工具
            Task.Run(() =>
            {
                Thread.Sleep(200);
                Console.WriteLine("Task 启动匿名方法");
            });
            Console.WriteLine("Task 默认不会阻塞");

            //获取 Task.Result 会造成阻塞等待task 执行
            int r = Task.Run(() =>
            {
                Console.WriteLine("Task启动执行匿名方法并返回值");
                Thread.Sleep(200);
                return 5;
            }).Result;
            Console.WriteLine("返回值是{0}", r);

            Program program = new Program();
            Stopwatch stopwatch = new Stopwatch();
            for (int i = 0; i < 50; i++)
            {
#pragma warning disable 4014
                program.MethodB(stopwatch, i);
#pragma warning restore 4014
                Console.WriteLine("普通方法{0}调用完成", i);
            }

            Stopwatch stopwatchb = new Stopwatch();
            for (int j = 0; j < 50; j++)
            {
#pragma warning disable 4014
                program.MethodA(stopwatchb, j);
#pragma warning restore 4014
                Console.WriteLine("异步方法{0}调用完成", j);
            }

            Console.ReadKey();
        }

        //异步和普通方法的区别,异步不需要等待线程执行结果。

        //异步方法
        public async Task<int> MethodA(Stopwatch stop, int i)
        {
            stop.Start();
            int result = await Task.Run(() =>
            {
                Console.WriteLine("异步方法{0}Task被执行", i);
                Thread.Sleep(100);
                return i * 2;
            });
            Console.WriteLine("异步方法{0}执行完毕,结果{1}", i, result);

            if (i == 49)
            {
                stop.Stop();
                Console.WriteLine("用时{0}", stop.ElapsedMilliseconds);
            }
            return result;
        }

        //普通方法
        public int MethodB(Stopwatch stop, int i)
        {
            stop.Start();
            int result = Task.Run(() =>
            {
                Console.WriteLine("普通多线程方法{0}Task被执行", i);
                Thread.Sleep(100);
                return i * 2;
            }).Result;
            Console.WriteLine("普通方法{0}执行完毕,结果{1}", i, result);

            if (i == 49)
            {
                stop.Stop();
                Console.WriteLine("用时{0}", stop.ElapsedMilliseconds);
            }
            return result;
        }
    }

 

posted @ 2016-08-17 16:05  落叶的瞬间;  阅读(229)  评论(0编辑  收藏  举报