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

简介: 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方法,效率高,速度快。


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#中开启多线程的方法,在后续学习中,会对每一种线程方法进行更深一步的介绍,希望大家多多关注。

相关文章
|
3天前
|
安全 Java C#
C#多线程详解
C#多线程详解
12 0
|
3天前
|
程序员 C#
C#抽象类和抽象方法详解
C#抽象类和抽象方法详解
6 0
|
3天前
|
存储 开发框架 .NET
C#中将DataTable转化成ListT的方法解析
C#中将DataTable转化成ListT的方法解析
5 0
|
4天前
|
数据采集 前端开发 数据挖掘
Fizzler库+C#:从微博抓取热点的最简单方法
本文介绍如何使用Fizzler库和C#构建微博热点信息爬虫。通过Fizzler的CSS选择器定位关键信息,提取热点标题和排名,实现微博内容的智能挖掘。示例代码展示单线程和多线程采集方法,并涉及代理IP使用。
Fizzler库+C#:从微博抓取热点的最简单方法
|
5天前
|
存储 数据采集 API
C# GetField 方法应用实例
C# GetField 方法应用实例
|
2天前
|
设计模式 消息中间件 安全
【Java多线程】关于多线程的一些案例 —— 单例模式中的饿汉模式和懒汉模式以及阻塞队列
【Java多线程】关于多线程的一些案例 —— 单例模式中的饿汉模式和懒汉模式以及阻塞队列
9 0
|
2天前
|
Java 数据库
【Java多线程】对线程池的理解并模拟实现线程池
【Java多线程】对线程池的理解并模拟实现线程池
10 1
|
2天前
|
Java
【Java多线程】分析线程加锁导致的死锁问题以及解决方案
【Java多线程】分析线程加锁导致的死锁问题以及解决方案
11 1
|
2天前
|
存储 缓存 安全
【Java多线程】线程安全问题与解决方案
【Java多线程】线程安全问题与解决方案
9 1
|
2天前
|
Java 调度
【Java多线程】线程中几个常见的属性以及状态
【Java多线程】线程中几个常见的属性以及状态
8 0