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从入门到精通:2.1.3深入学习Java核心技术——掌握Java多线程编程
Java从入门到精通:2.1.3深入学习Java核心技术——掌握Java多线程编程
|
7天前
使用代理IP池实现多线程的方法
使用代理IP池实现多线程的方法
|
28天前
|
Java 测试技术 Python
Python开启线程和线程池的方法
Python开启线程和线程池的方法
17 0
Python开启线程和线程池的方法
|
1月前
|
数据处理 调度 开发者
QML多线程魔法:探索不同方法,提升性能
QML多线程魔法:探索不同方法,提升性能
179 0
|
1月前
|
C#
C#学习系列相关之多线程(二)----Thread类介绍
C#学习系列相关之多线程(二)----Thread类介绍
|
17天前
|
存储 Java 数据库连接
java多线程之线程通信
java多线程之线程通信
|
29天前
|
存储 缓存 NoSQL
Redis单线程已经很快了6.0引入多线程
Redis单线程已经很快了6.0引入多线程
31 3
|
1月前
|
消息中间件 安全 Linux
线程同步与IPC:单进程多线程环境下的选择与权衡
线程同步与IPC:单进程多线程环境下的选择与权衡
58 0
|
1月前
|
安全 编译器 C#
C#学习相关系列之多线程---lock线程锁的用法
C#学习相关系列之多线程---lock线程锁的用法
|
1月前
|
Java C#
C#学习系列相关之多线程(五)----线程池ThreadPool用法
C#学习系列相关之多线程(五)----线程池ThreadPool用法