C# 线程

1 篇文章 0 订阅
这篇博客介绍了线程的基本概念,包括线程作为程序执行路径的重要性,以及为何使用线程来优化数据查询和提高应用效率。文章展示了如何在C#中通过四种方式创建线程:静态方法、实例方法、匿名委托和Lambda表达式,并给出了具体的代码示例。
摘要由CSDN通过智能技术生成

         自己学习了解一下线程,是因为之前要导出很大数据的Excel,当时在网上百度到了二维数组导入的方式,写入速度是快了,但是前期的查询数据量的优化就只能靠分批查询(线程),所以就了解了下,但后面没做,等有时间再去填这个小工具吧。

打印线程的一些数据

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

namespace MyThread
{
    internal class MyTest
    {
        /// <summary>
        /// 主线程 部分数据查看
        /// view part info of the main thread
        /// </summary>
        public static void mainThread()
        {
            Thread th = Thread.CurrentThread;
            Console.WriteLine("Name:" + th.Name);
            Console.WriteLine("ManagedThreadId:" + th.ManagedThreadId);
            Console.WriteLine("IsBackground:" + th.IsBackground);
            // 指的是用的那种语言,或者输入法 zh-Tw
            Console.WriteLine("CurrentCulture:" + th.CurrentCulture);
            th.Name = "Killen";
            Console.WriteLine("Name:" + th.Name);
            Console.WriteLine("Hello World!");
            Console.WriteLine("ManagedThreadId:" + th.ManagedThreadId);
            Console.WriteLine("This is MainThread:" + th.Name);
        }


    }
}

 怎么创建一个线程

using System;
using System.Threading;

namespace MyThread
{
    /// <summary>
    /// 1.what 
    ///     Threads are defined as the execuation path of a program.
    /// 2.why
    ///     Threads are lightweight processes(进程), can save waste of CPU cycles, 
    ///     as well as improving/and improve the efficiency of the application.
    /// 3.how 
    ///     4 ways to create a thread
    ///     a. static method
    ///     b. instance 
    ///     c. delegate
    ///     d. lambda
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            MyTest.mainThread();
            Console.WriteLine("***************************************************************************");

            /* 
                 // 1. 运行静态的方法
                 Thread th = new Thread(new ParameterizedThreadStart(ChildTest));
            */


            // 2. 运行实例方法 
            MyThreadTest test = new MyThreadTest();
            Thread th = new Thread(new ParameterizedThreadStart(test.ChildTest));


            Console.WriteLine(th.ManagedThreadId);
            Console.WriteLine("I'm creating a thread.");
            th.Start(2);
            Console.WriteLine("The thread is start.");



            // 3. 通过匿名委托创建
            Thread thDelegate = new Thread(delegate (object o)
            {
                Console.WriteLine("I'm a childThread with delegate." + o);
            });
            thDelegate.Start(1);


            // 4. 通过Lambda表达式创建
            Thread thLambda = new Thread((object o) =>
            {
                Console.WriteLine("I'm a childThread with Lambda." + o);
            });
            thLambda.Start(0);
            Console.WriteLine("The thread is start.");


        }

        /// <summary>
        /// 1. 运行静态的方法
        /// 创建有参的方法
        /// </summary>
        /// <param name="o"></param>
        static void ChildTest(object o)
        {
            Console.WriteLine("I'm a childThread with static." + o);
        }


        /// <summary>
        /// 2. 运行实例方法
        /// </summary>
        class MyThreadTest
        {
            /// <summary>
            /// 1.创建有参的方法
            /// </summary>
            /// <param name="o"></param>
            public void ChildTest(object o)
            {
                Console.WriteLine("I'm a childThread with instance." + o);
            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值