线程学习

  class Program
    {


        static SemaphoreSlim _sem = new SemaphoreSlim(3);
        private static bool _isDone = false;

        private static object _lock = new object();
        static void Main(string[] args)
        {

            _sem.Wait();
            _sem.Release();
            //new Thread(go).Start();
            //Task.Factory.StartNew(go);
            //Task.Run(new Action(go));
            //var aa = Thread.CurrentThread.ManagedThreadId;

            //Console.WriteLine("我是主线程:编号{0}", aa);
            //ThreadPool.QueueUserWorkItem(go);

            //Task.Run(() =>
            //{
            //    Go("小米");
            //});

            //var aa = Task.Run<string>(() =>
            //{
            //    return GO("小米");
            //});


            //var aa = GetList2();

           // var dd = Task.Run(() =>
           //{
           //    return GetList2();
           //}).Result;
            
           // foreach (var item in dd.Distinct())
           // {

           // };
           // db.SaveChanges();
           //var cc = GetList();
            //foreach (var item in dd)
            //{
            //    Console.WriteLine(item.Id+item.Name);
            //}

            //Task.Run(() =>
            //{
            //    Done();
            //});
            //Task.Run(() =>
            //{
            //    Done();
            //});
            //new Thread(Done).Start();
            //new Thread(Done).Start();
            //new Thread(Done2).Start();
            //new Thread(Done2).Start();
            //Task.Run(() =>
            //{

            //    GetNum();
            //});

            Console.ReadKey();
        }

        private async static void GetNum()
        {
            for (int i = 0; i < 1000; i++)
            {
                Console.Write("'"+i+"'");
            }
        }

        static void Enter(object id)
        {
            Console.WriteLine(id + " 开始排队...");
            _sem.Wait();
            Console.WriteLine(id + " 开始执行!");
            Thread.Sleep(1000 * (int)id);
            Console.WriteLine(id + " 执行完毕,离开!");
            _sem.Release();
        }

        static void Done2()
        {
            lock (_lock)
            {
                if (!_isDone)
                {
                    Console.WriteLine("Done");
                    _isDone = true;
                }
            }
        }

        static void Done()
        {
            if (!_isDone)
            {
                _isDone = true;
                Console.WriteLine("done");
            }
        }

        public static string GO(string str)
        {
            return "我是:" + str;
        }

        public static  List<Student> GetList2()
        {

            return new List<Student>()
            {
                new Student{Id=1,Name="小米"},
                new Student{Id=2,Name="小米2"},
                new Student{Id=3,Name="小米3"}
            };
        }


        public static async Task<List<Student>> GetList()
        {

            return await Task.Run(() =>
            {
                return new List<Student>() {
                    new Student{Id=1,Name="小米"},
                    new Student{Id=2,Name="小米2"},
                    new Student{Id=3,Name="小米3"}

                };
            });
        }

        //public static void go() {
        //    Console.WriteLine("我是线程:");
        //}
        //public static void go(object str)
        //{
        //    Console.WriteLine("我是线程:"+Thread.CurrentThread.ManagedThreadId);
        //}


        //public static void Go(object name)
        //{
        //    Console.WriteLine("名字:{0}",name);
        //}
    }

    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

 

posted @ 2018-03-02 16:43  苑志飞  阅读(173)  评论(0)    收藏  举报