学习C#异步编程

时间:2020-07-11 12:57:46   收藏:0   阅读:66

"杨老师视频教程"

P1 线程(Thread):创建线程

视频地址

什么是线程Thread

例子:

 class Program
    {
        static void Main(string[] args)
        {
            Thread thread = new Thread(WriteY); //开启一个新的线程 Thread
            thread.Name = "Y Thread...";
            thread.Start();

            for (int i = 0; i < 1000; i++)
                Console.WriteLine("x");
            Console.ReadKey();
        }

        private static void WriteY()
        {
            for(int i = 0; i < 1000; i++)
            {
                Console.WriteLine("y");
            }
        }
    }

术语:线程被抢占

线程的一些属性

P2 Thread.Join()&Thread.Sleep()

视频地址

Join and Sleep

class Program
    {
        static void Main(string[] args)
        {
            Thread t = new Thread(WriteY);
            t.Start();
            t.Join();//当前线程会等待t线程执行结束
            Console.WriteLine("线程结束");
            Console.Read();
        }

        private static void WriteY()
        {
            for(int i = 0; i < 1000; i++)
            {
                Console.Write("y");
            }
        }
    }
class Program
    {
        static Thread thread1, thread2;
        static void Main(string[] args)
        {
            thread1 = new Thread(ThreadProc);
            thread1.Name = nameof(thread1);
            thread1.Start();

            thread2 = new Thread(ThreadProc);
            thread2.Name = nameof(thread2);
            thread2.Start();
            Console.Read();
        }

        private static void ThreadProc()
        {
            Console.WriteLine($"\nCurrent Thread:{Thread.CurrentThread.Name}");
            if (Thread.CurrentThread.Name == nameof(thread1) &&
                thread2.ThreadState != ThreadState.Unstarted)
                thread2.Join();
            Thread.Sleep(4000);
            Console.WriteLine($"\nCurrent thread:{Thread.CurrentThread.Name}");
            Console.WriteLine($"Thread1:{thread1.ThreadState}");
            Console.WriteLine($"Thread2:{thread2.ThreadState}");
        }
    }

输出内容:

Current Thread:thread1

Current Thread:thread2

Current thread:thread2
Thread1:WaitSleepJoin
Thread2:Running

Current thread:thread1
Thread1:Running
Thread2:Stopped

添加超时

例子:

class Program
    {
        static Thread thread1, thread2;
        static void Main(string[] args)
        {
            thread1 = new Thread(ThreadProc);
            thread1.Name = nameof(thread1);
            thread1.Start();

            thread2 = new Thread(ThreadProc);
            thread2.Name = nameof(thread2);
            thread2.Start();
            Console.Read();
        }

        private static void ThreadProc()
        {
            Console.WriteLine($"\nCurrent Thread:{Thread.CurrentThread.Name}");
            if (Thread.CurrentThread.Name == nameof(thread1) &&
                thread2.ThreadState != ThreadState.Unstarted)
                if (thread2.Join(2000))
                    Console.WriteLine("线程2结束");
                else
                    Console.WriteLine("线程2超时了");
            Thread.Sleep(4000);
            Console.WriteLine($"\nCurrent thread:{Thread.CurrentThread.Name}");
            Console.WriteLine($"Thread1:{thread1.ThreadState}");
            Console.WriteLine($"Thread2:{thread2.ThreadState}");
        }
    }

输出:

Current Thread:thread1

Current Thread:thread2
线程2超时了

Current thread:thread2
Thread1:WaitSleepJoin
Thread2:Running

Current thread:thread1
Thread1:Running
Thread2:Stopped

Sleep(0)或Yield有时在高级性能调试的生产代码中很有用。它也是一个很好的诊断工具,有助于发现线程安全问题:

如果在代码中的任何地方插入Thread.Yield()就破坏了程序,那么你的程序几乎肯定有bug。

P3 阻塞Blocking

视频地址

阻塞

ThreadState

P4 什么是线程安全

视频地址

本地 vs 共享的状态

Local 本地独立

Shared共享

线程安全 Thread Safety

锁定与线程安全简介

P5 向线程传递数据&异常处理

视频地址

向线程传递数据

向线程传递数据在C#3.0之前

Lambda表达式与被捕获的变量

异常处理

P6 前台线程 vs 后台线程

视频地址

Foreground vs Background Threads

P7 线程优先级

视频地址

线程优先级

提升线程优先级

P8 信号介绍

视频地址

信号 Signaling

P9 富客户端应用处理耗时操作的一种办法

视频地址

富客户端应用程序的线程

P10 Synchronization Context

视频地址

Synchronization Contexts 同步上下文

P11 线程池

视频地址

线程池 Thread Pool

使用线程池需要注意的几点

进入线程池

谁使用了线程池

线程池中的整洁

CLR的策略

P12 开始一个Task

视频地址

Thread的问题

Task Class

开始一个Task

Task.Run

Wait 等待

Long-running tasks 长时间运行的任务

P13 Task的返回值

视频地址

Task的返回值

P14 Task的异常

视频地址

Task异常

异常与“自治”的Task

未观察到的异常

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!