简单多线程、线程池与异步多线程

  • 异步多线程
  • 异步的执行顺序
  • Thread
  • 线程池

异步多线程
常见的同步方法:

using System;

namespace AsyncPractice
{
    internal class AsyncClass
    {
        public AsyncClass()
        {
            Action action = () => this.DoSomething();
            action.Invoke();
        }

        private void DoSomething()
        {
            Console.WriteLine("DoSomething");
        }
    }
}

常见的异步方法:

using System;
using System.Threading;

namespace AsyncPractice
{
    internal class AsyncClass
    {
        public AsyncClass()
        {
            //异步方法
            {
                Action action = () => this.DoSomething();
                action.BeginInvoke(null, null);
            }
            //异步方法 返回值为第二个参数
            {
                Action action = () => this.DoSomething();
                var actionReturn = action.BeginInvoke(null, 1);
                Console.WriteLine(actionReturn.AsyncState);
            }
            //CallBack
            {
                Action action = () => this.DoSomething();
                AsyncCallback callbcak = t =>
                {
                    Console.WriteLine(t.ToString());
                    Console.WriteLine("CallBack");
                };
                var actionReturn = action.BeginInvoke(callbcak, 2);
                Console.WriteLine(actionReturn.AsyncState);
            }
            //等待异步结束
            {
                Action action = () => this.DoSomething();
                AsyncCallback callbcak = t =>
                {
                    Console.WriteLine(t.ToString());
                    Console.WriteLine("CallBack");
                };
                var actionReturn = action.BeginInvoke(callbcak, 2);
                Console.WriteLine(actionReturn.AsyncState);
                while (!actionReturn.IsCompleted)
                {
                    Thread.Sleep(100);
                    Console.WriteLine("Async running...");
                }
                Console.WriteLine("wait");
            }
        }

        private void DoSomething()
        {
            Thread.Sleep(1000);
            Console.WriteLine("DoSomething");
        }
    }
}

异步的执行顺序
  普通异步的执行顺序。
在这里插入图片描述
  BeginInvoke返回值的执行顺序:
在这里插入图片描述
  CallBack执行顺序:
在这里插入图片描述
 emsp;带有阻塞的异步:
在这里插入图片描述
多线程

using System;
using System.Threading;

namespace ThreadPractice
{
    internal class ThreadClass
    {
        public ThreadClass()
        {
            {
                //无参多线程
                Thread thread = new Thread(DoSomething);
                thread.IsBackground = true;
                thread.Start();
            }
            {
                //有参多线程(ThreadStart)
                for (int i = 0; i < 10; i++)
                {
                    string name = string.Format("Thread{0}", i);
                    ThreadStart threadStart = () => this.DoSomething(name);
                    Thread thread = new Thread(threadStart);
                    thread.Start();
                }
            }
            {
                //有参多线程(ParameterizedThreadStart)
                for (int i = 0; i < 10; i++)
                {
                    string name = string.Format("Thread{0}", i);
                    ParameterizedThreadStart param = t => this.DoSomething(name);
                    Thread thread = new Thread(param);
                    thread.Start();
                }
            }
        }

        private void DoSomething()
        {
            Console.WriteLine("Thread Start 当前线程:{0},时间:{1},", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
            long sum = 0;
            for (int i = 1; i < 999999999; i++)
            {
                sum += i;
            }
            Console.WriteLine("TestThread End 当前线程:{0},时间:{1},i:{2}", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), sum);
        }

        private void DoSomething(string threadName)
        {
            Console.WriteLine("Thread Start  Name={2},当前线程:{0},时间:{1},", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), threadName);
            long sum = 0;
            for (int i = 1; i < 999999999; i++)
            {
                sum += i;
            }
            Console.WriteLine("TestThread End  Name={2},当前线程:{0},时间:{1},i:{3}", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), threadName, sum);
        }
    }
}

线程池

using System;
using System.Threading;

namespace ThreadPractice
{
    internal class ThreadClass
    {
        public ThreadClass()
        {
            {
                WaitCallback wait = t => this.DoSomething();
                ThreadPool.QueueUserWorkItem(wait);
            }
            {
                WaitCallback wait = t => this.DoSomething(t.ToString());
                ThreadPool.QueueUserWorkItem(wait, "QueueName");
            }
            {
            	//阻塞
                ManualResetEvent mre = new ManualResetEvent(false);
                WaitCallback wait = t =>
                {
                    mre.Set();
                    Console.WriteLine("{0}", t.ToString());
                };
                ThreadPool.QueueUserWorkItem(wait, "Param T");

                Console.WriteLine("DoSomething...");
                Console.WriteLine("DoSomething...");
                Console.WriteLine("DoSomething...");
                Console.WriteLine("DoSomething...");
                mre.WaitOne();
            }
        }

        private void DoSomething()
        {
            Console.WriteLine("Thread Start 当前线程:{0},时间:{1},", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
            long sum = 0;
            for (int i = 1; i < 999999999; i++)
            {
                sum += i;
            }
            Console.WriteLine("TestThread End 当前线程:{0},时间:{1},i:{2}", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), sum);
        }

        private void DoSomething(string threadName)
        {
            Console.WriteLine("Thread Start  Name={2},当前线程:{0},时间:{1},", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), threadName);
            long sum = 0;
            for (int i = 1; i < 999999999; i++)
            {
                sum += i;
            }
            Console.WriteLine("TestThread End  Name={2},当前线程:{0},时间:{1},i:{3}", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), threadName, sum);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值