zoukankan      html  css  js  c++  java
  • C#多线程的几种实现方法

    1.最简单的多线程

    using System;
    using System.Threading;
    
    namespace ThreadTest1
    {
        public class SimpleThread
        {
            public void Run()
            {
                Thread t1 = new Thread(new ThreadStart(this.ThreadFunc));
                Thread t2 = new Thread(new ThreadStart(this.ThreadFunc));
                t1.Start();
                Thread.Sleep(1000);
                t2.Start();
            }
    
            private void ThreadFunc()
            {
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine(i);
                    Thread.Sleep(100);
                }
            }
        }
    }
    
    

    2.可以传参数的多线程

    using System;
    using System.Threading;
    
    namespace ThreadTest1
    {
        class ThreadWithPara
        {
            public void Run()
            {
                Thread t1 = new Thread(ThreadFunc);
                Thread t2 = new Thread(new ParameterizedThreadStart(ThreadFunc));
                t1.Start("t1");
                Thread.Sleep(1000);
                t2.Start("t2");
            }
    
            private void ThreadFunc(object obj)
            {
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine(i + obj.ToString());
                    Thread.Sleep(100);
                }
            }
        }
    }
    
    

    3.自定义类的多线程

    using System;
    using System.Threading;
    
    namespace ThreadTest1
    {
        class MyThreadClass
        {
            private string data;
    
            public MyThreadClass(string data)
            {
                this.data = data;
            }
    
            public void ThreadMain()
            {
                Console.WriteLine("Running in a thread,data: {0}", data);
            }
        }
    
        class MainClass
        {
            public void Run()
            {
                MyThreadClass mtc = new MyThreadClass("thread1");
                Thread thread = new Thread(mtc.ThreadMain);
                thread.Start();
            }
        }
    }
    
    

    4. 匿名方法实现的多线程

    using System;
    using System.Threading;
    
    namespace ThreadTest1
    {
        public class AnonymousThread
        {
            public void Run()
            {
                Thread t1 = new Thread(() => ThreadFunc("thread-1"));
                t1.Start();
            }
    
            private void ThreadFunc(string obj)
            {
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine(i + obj.ToString());
                    Thread.Sleep(100);
                }
            }
        }
    }
    
    

    5.参考

    C# 给多线程传参的三种方式

  • 相关阅读:
    封装的图片预加载,数据加载到浏览器底部加载数据
    自己封装的弹出层插件
    在规定的时间内出现动画.html
    WEB前端资源集
    前端优化几项
    移动H5前端性能优化指南
    微信小程序IDE(微信web开发者工具)安装、破解手册--转载
    微信小程序开发—快速掌握组件及API的方法---转载
    STM32数据类型定义
    HDOJ 4802 GPA
  • 原文地址:https://www.cnblogs.com/wardensky/p/4381930.html
Copyright © 2011-2022 走看看