A17_多线程


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;

namespace A17_Multithreading
{


    class MyThread
    {
        private string _FilePath;
        private string _FileName;

        public MyThread(string path, string fileName)
        {
            _FilePath = path;
            _FileName = fileName;
        }

        public void DownFileDwnLoad()
        {
            Console.WriteLine("开始下载:" + _FilePath + "/" + _FileName);
            Thread.Sleep(2000);
            Console.WriteLine("下载完毕");
        }
    }
}


/*
 * 多线程一般定义方式
 * 执行的顺序没有规律
 * 前台线程可以在任何时候改为后台线程
 * 
 * 前台线程适用于比较重要核心的功能,需要长时间等待的任务
 * 适用于自动执行且非核心的功能(拼写检查,游戏载入资源时的背景动画)
 * 
 * 
 * **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;  //多线程命名空间

namespace A17_Multithreading
{
    class Demo
    {

        public void ThreadMethod_1()
        {
            //Console.WriteLine("运行线程1");
            for (int i = 0; i < 10000; i++)
            {
                Console.Write("线程1");
            }
            
        }

        public void ThreadMethod_2()
        {
            //Console.WriteLine("运行线程2");
            for (int i = 0; i < 10000; i++)
            {
                Console.Write("线程2");
            }
        }

        public void ThreadMethod_2_1()
        {
            //Console.WriteLine("运行线程2");
            for (int i = 0; i < 10000; i++)
            {
                Console.Write("线程2_1");
            }
        }

        public void DownLoadFile(object fileName)
        {
            Console.WriteLine("文件名:"+fileName.ToString());
            Console.WriteLine("下载文件:" + Thread.CurrentThread.ManagedThreadId);
            Thread.Sleep(1000);
            Console.WriteLine("下载完毕");
        }

        //一般定义多线程方法
        public void Test1()
        {
            Thread t1 = new Thread(ThreadMethod_1);
            t1.Start();

            Thread t2 = new Thread(ThreadMethod_2);
            t2.Start();
        }

        //用lambda表达式开启多线程
        public void Test2()
        {
            Thread t1 = new Thread(
                                     () =>
                                     {
                                         for (int i = 0; i < 10000; i++)
                                         {
                                             Console.Write("线程1");
                                         }
                                     }
                                  );
            t1.Start();

            Thread t2 = new Thread(
                                     () =>
                                     {
                                         for (int i = 0; i < 10000; i++)
                                         {
                                             Console.Write("线程2");
                                         }
                                     }
                                );
            t2.Start();
        }

        public void ThreadMethod_3()
        {
            for (int i = 0; i < 10000; i++)
            {
                Console.Write("线程3");
            }
        }

        public void ThreadMethod_4(int num)
        {
            for (int i = 0; i < 10000; i++)
            {
                Console.Write(" 线程4 参数 = num ");
            }
        }

        public int DownLoadMoreFile(int num)
        {
            Console.WriteLine("文件下载中");
            Thread.Sleep(2000);
            return num + 2000;
        }

        //线程的传参
        public void Test3()
        {
            Thread t = new Thread(DownLoadFile);
            t.Start("abc.mov"); //启动带参线程
        }

        //线程传递多个参数
        public void Test4()
        {
            MyThread mythObj = new MyThread(@"D:\filePath","abc.mov");
            Thread t1 = new Thread(mythObj.DownFileDwnLoad);
            t1.Start();
        }

        //委托开启线程
        //委托开启的线程都是所谓的后台线程
        //后台线程会随着前台线程的关闭而关闭
        //所以,如果前台线程运行非常快,就会导致后台线程提前关闭
        //因此在本方法中需要使用Console.ReadLine()来使前台主线程挂起。
        public void Test5()
        {
            Action ac = ThreadMethod_3;
            ac.BeginInvoke(null, null);
            //Console.ReadLine();
        }

        //带参数委托启动线程
        public void Test6()
        {
            Action ac = ThreadMethod_4;
            ac.BeginInvoke(88, null, null);
            Console.ReadLine();
        }


        //委托开启线程且接收返回值 使用“状态结果”
        public void Test7()
        {
            Func fu = DownLoadMoreFile;

            //IAsyncResult  表示异步调用的状态
            IAsyncResult result = fu.BeginInvoke(100,null,null);

            while (!result.IsCompleted)
            {
                Console.Write(".");
                Thread.Sleep(10);
            }

            //取得结果
            int intResult = fu.EndInvoke(result);
            Console.WriteLine("线程返回值 = " + intResult);
            Console.ReadLine();
        }

        //委托开启线程且接收返回值,使用等待句柄
        public void Test8()
        {
            Func fu = DownLoadMoreFile;

            //IAsyncResult  表示异步调用的状态
            IAsyncResult result = fu.BeginInvoke(100, null, null);

            bool boResult = result.AsyncWaitHandle.WaitOne(5000);//等待5秒

            if (boResult)
            {
                int intResult = fu.EndInvoke(result);
                Console.WriteLine("使用“等待句柄”方式,线程返回的数值 = " + intResult);
            }
            else
            {
                Console.WriteLine("规定时间内么有的到子线程的结果");
            }
            //Console.ReadLine();
        }

        //委托开启线程,且接收返回值,使用回调函数
        public void Test9()
        {
            Console.WriteLine("使用“回调函数”方式");
            Func fu = DownLoadMoreFile;
            //OnCallBack 是回调函数,当子线程结束的时候,自动调用
            fu.BeginInvoke(200, OnCallBack, fu);
            Console.ReadLine();
        }

        void OnCallBack(IAsyncResult ar)
        {
            Func res = ar.AsyncState as Func;
            int intResult = res.EndInvoke(ar);
            Console.WriteLine("在回调函数中取得结果 = " + intResult);

        }

        //演示线程的优先级
        public void Test10()
        {
            Thread t1 = new Thread(ThreadMethod_1);
            Thread t2 = new Thread(ThreadMethod_2);
            Thread t2_1 = new Thread(ThreadMethod_2_1);
            t1.Start();
            t2.Start();
            t2_1.Start();

            //线程优先级控制
            t2_1.Priority = ThreadPriority.Highest;
        }

        //线程的状态控制
        public void Test11()
        {
            Thread t1 = new Thread(ThreadMethod_1);
            t1.Start();
            //t1.Sleep(1000);
            t1.Abort();
            t1.Join(1000); //指定的线程实例等待
            Thread.Sleep(1000); //当前线程休眠
        }

        //后台线程
        public void Test12()
        {
            //DownLoadFile
            Thread t1 = new Thread(DownLoadFile);
            t1.Start("abc.mov");
            //把当前线程变为“后台线程”
            t1.IsBackground = true;
            Console.ReadLine();
        }

        //主线程
        static void Main(string[] args)
        {
            Demo obj = new Demo();
            //obj.Test1();
            //obj.Test2();
            //obj.Test3();
            //obj.Test4();
            //obj.Test5();
            //obj.Test6();
            //obj.Test7();
            //obj.Test8();
            //obj.Test9();
            //obj.Test10();
            //obj.Test11();
            obj.Test12();
        }
    }
}








你可能感兴趣的:(C#学习笔记)