自己学习了解一下线程,是因为之前要导出很大数据的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);
}
}
}
}