C# 多线程编程三大核心基础特点winform例子

         /*总结,多线程和普通编程最大区别:
         * 第一个:同步线程卡界面,
         * 关闭界面,点击界面都是由主线程完成,当进行计算时主线程无暇处理点击,拖动界面等
         * 
         * 第二个:同步线程比较慢,执行五次10亿次计算,花费将近12秒,只有一个线程
         * 异步线程比较快,花费5秒时间,一共五个线程,性能提升,并不是线性,提升2.4倍
         * 因此并不是线程越多越好,一来资源有限,二来存在多线程协调成本
         * 相当于个体老板和董事长区别,

         资源换时间,性能

C# 多线程编程三大核心基础特点winform例子_第1张图片
         * 
         * 第三个:无序性,不可预测性
         * 即使同一个线程相同任务执行时间也不同,本质是因为程序向操作系统申请线程,以及计算都需要操作系统来调度CPU处理,
         * 这个CPU是分片的,现在计算机CPU计算能力都很强,一秒几亿,分成等分,随机进行处理,线程优先级可以影响调度顺序
         * 线程是属于计算机的资源,CLR向操作系统请求资源,
         * 这是多线程核心基础
         * 
         * 不要试图用延时等方式,控制顺序,更加不要

试图用各种‘风骚’所谓的巧妙方式彻底控制顺序,

墨菲:任何小概率的坏事情,随着时间的推移,一定会发生,
         */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AsyncVSsync
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //private   Action action =this. DoSomethingLong;
        private void button1_Click(object sender, EventArgs e)
        {
            Action action = this.DoSomethingLong;
            Console.WriteLine("同步方法 start id={0}",Thread.CurrentThread.ManagedThreadId);
            int j = 0;
            int k = 1;
            int m = j + k;
            for (int i = 0; i < 5; i++)
            {
                string name = "button1_Click" + i;
                action.Invoke(name);

            }
            Console.WriteLine("同步方法 end id={0}", Thread.CurrentThread.ManagedThreadId);
        }

        private void DoSomethingLong(string name) {
            Console.WriteLine("*****do something long start name={0},ID={1},Datetime={2}", name, Thread.CurrentThread.ManagedThreadId,DateTime.Now.ToString("HH:ss:mm fff"));
            long result = 0;
            for (int i = 0; i < 1000000000; i++)
            {
                result += i;
            }
            Console.WriteLine("*****do something long end name={0},ID={1},Datetime={2},result={3}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("HH:ss:mm fff"),result);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Console.WriteLine("异步方法 start id={0}", Thread.CurrentThread.ManagedThreadId);
            Action action = this.DoSomethingLong;
            //将一个方法赋值给他,而不是调用它
            action.Invoke("委托action.Invoke()执行一个同步方法");
            action("直接像方法一样调用委托,action()执行一个同步方法");
            action.BeginInvoke("action.BeginInvoke()这是异步调用委托,借助的是委托提供的一个方法", null, null);
            Console.WriteLine("异步方法 end id={0}", Thread.CurrentThread.ManagedThreadId);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Action action = this.DoSomethingLong;
            Console.WriteLine("异步方法 start id={0}", Thread.CurrentThread.ManagedThreadId);
            int j = 0;
            int k = 1;
            int m = j + k;
            for (int i = 0; i < 5; i++)
            {
                string name = "button1_Click" + i;
                action.BeginInvoke(name,null,null);
            }
            Console.WriteLine("异步方法 end id={0}", Thread.CurrentThread.ManagedThreadId);
        }
    }
}

关于多线程编程,感受体验以上三点,看朝夕教育Eleven老师,确实收获颇丰,记录于此

备注:C# 与Thread,与计算机线程,进程本质关系

 线程进程本质:
进程是计算机操作系统分配资源的单位,线程可与同一线程共享全部资源
进程和进程之间,通信很困难,(分布式)
线程是进程的实体,不能单独存在,依托程序存在,他是CPU调度分配的基本单位

同一时间,允许两个或以上进程处于运行状态,就是多任务,但是它是基于时间片轮转进程调度算法,
一个电脑cpu,理论上同一时间,有且仅有一个进程cpu

进程和线程是基于操作系统,和计算机,程序运行在计算机的操作系统上,
因此不是程序,创造出线程,它只是借助Thread对线程进行封装,进而调用线程,

你可能感兴趣的:(c#)