C#学习系列相关之多线程(一)----常用多线程方法总结

一、多线程的用途

        在介绍多线程的方法之前首先应当知道什么是多线程, 在一个进程内部可以执行多个任务,而这每一个任务我们就可以看成是一个线程。是程序使用CPU的基本单位。进程是拥有资源的基本单位, 线程是CPU调度的基本单位。多线程的作用不是提高执行速度,而是为了提高应用程序的使用率。我们程序在运行的使用,都是在抢CPU的时间片(执行权),如果是多线程的程序,那么在抢到
CPU的执行权的概率应该比较单线程程序抢到的概率要大.那么也就是说,CPU在多线程程序中执行的时间要比单线程多,所以就提高了程序的使用率.但是即使是多线程程序,那么他们中的哪个线程能抢占到CPU的资源呢,这个是不确定的,所以多线程具有随机。
       多线程就好比在等待水开的同时看报纸,而不是等水开了再开始看报纸。多线程是为了同步完成多项任务,而是为了提高资源使用效率来提高系统的效率。(这个例子并不是很恰当,可以简单理解为水开和看报纸交替执行,交替的速度极快,进而可以看作是两个任务同时执行的)。

二、常用多线程的方法

1、Thread类

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

namespace 线程test1005
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 300; i++)
            {
                Console.Write(1);
            }
            Thread t1 = new Thread(() =>
            {
                for (int i = 0; i < 300; i++)
                {
                    Console.Write(2);
                } });
            t1.Start();
            for (int i = 0; i < 300; i++)
            {
                Console.Write(3);
            }
            Console.Read();
        }
    }
}

   运行结果:

2、通过Task类(最常用的方法)

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

namespace 线程test1005
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 300; i++)
            {
                Console.Write(1);
            }
            Task t1 = new Task(() =>
            {
                for (int i = 0; i < 300; i++)
                {
                    Console.Write(2);
                } });
            t1.Start();
            for (int i = 0; i < 300; i++)
            {
                Console.Write(3);
            }
            Console.Read();
        }
    }
}

运行结果:

在C#中多线程的方法主要就是Task方法,效率高,速度快。

提示:本人准备建立一个技术交流群,会将日常学习工作中遇到的问题和解决方案进行分享,同时也会将代码和学习资料上传进去,有什么不懂的问题可以咨询我!+v:SJS66-12

生活所迫打个广告,本人也代购莆田鞋,不是中间商,工厂直接取货,价格优惠质量保证,都是我自己前去挑选,可以视频选购验货!!希望大家支持!!!赚点生活费!!!+v:SJS66-12

 3、线程池ThreadPool类

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

namespace 线程test1005
{
    class Program
    {
        static void Main(string[] args)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(TestThreadPool), new string[] { "test" });
            for (int i = 0; i < 300; i++)
            {
                Console.Write(2);
            }
            Console.ReadKey();
        }
        public static void TestThreadPool(object state)
        {
            string[] arry = state as string[];//传过来的参数值
            int workerThreads = 0;
            int CompletionPortThreads = 0;
            for (int i = 0; i < 300; i++)
            {
                Console.Write(1);
            }
            ThreadPool.GetMaxThreads(out workerThreads, out CompletionPortThreads);
            Console.WriteLine(DateTime.Now.ToString() + "---" + arry[0] + "--workerThreads=" + workerThreads + "--CompletionPortThreads" + CompletionPortThreads);
        }
    }
}

运行结果:

4、通过begininvoke方法

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

namespace 线程test1005
{
    class Program
    {
         static void Main(string[] args)
        {
            Action a = teat;
            a.BeginInvoke(null, null);
            for (int i = 0; i < 300; i++)
            {
                Console.Write(2);
            }
            Console.ReadKey();
        }
        static void teat()
        {
            for (int i = 0; i < 300; i++)
            {
                Console.Write(1);
            }
        }
    }
}

运行结果:

5、async和await方法

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

namespace 线程test1005
{
    class Program
    {
         static void Main(string[] args)
        {
            test();
            for (int i = 0; i < 300; i++)
            {
                Console.Write(3);
            }
            Console.Read();
        }
        public static async void test()
        {
            for (int i = 0; i < 300; i++)
            {
                Console.Write(1);
            }
            await Task.Run(()=> {
                for (int i = 0; i < 300; i++)
                {
                    Console.Write(2);
                }
            });
        }
    }
}

运行结果:

本文介绍这几种C#中开启多线程的方法,在后续学习中,会对每一种线程方法进行更深一步的介绍,希望大家多多关注。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值