线程、线程池、Timer、事件的理解与区别

首先明确什么时候用多线程?

多线程是提高cpu的利用率,只有当cpu空闲时间比较多情况下,才能体现出多线程的优势。

线程:线程是进程的组成单位。

主要步骤:

① 实例化ThreadStart对象,参数是线程将要执行的方法。

② 编写线程将要执行的方法。

③ 实例化Thread对象,参数是刚才实例化ThreadStart的对象。

④ Thread对象启动,

线程的例子:

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

namespace ThreadTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private static int interval;

private void Form1_Load(object sender, EventArgs e)
{
interval = 1000;

Thread t = Thread.CurrentThread;
t.Name = "主要线程";

ThreadStart ts1 = new ThreadStart(ExampleMethod);

Thread t1 = new Thread(ts1);
t1.Name = "工作线程";

t1.Start();

ExampleMethod();
Console.ReadLine();
}

public void ExampleMethod() {

Thread t = Thread.CurrentThread;
string name = t.Name;

lock (this)
{
for (int i = 0; i <= 8 * interval; i++)
{
if (i % interval == 0)
{
Console.WriteLine("线程{0}计算器:{1}", name, i);
}
}

}


}
}
}

线程池:比线程在使用资源的方面更优化合理一些。

主要步骤:

① 实例化WaitCallback一个对象,参数是要执行的线程池的方法。

② 编写线程池要执行的方法。

③ 把任务排入执行的队列,例如ThreadPool.QueueUserWorkItem(wc1, 1);。

线程池的例子:

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

namespace Thread
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
WaitCallback wc1 = new WaitCallback(ExampleMethod);
WaitCallback wc2 = new WaitCallback(ExampleMethodTwo);

ThreadPool.QueueUserWorkItem(wc1, 1);
ThreadPool.QueueUserWorkItem(wc2, 2);

Console.ReadLine();
}

public static void ExampleMethod(object status)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("线程{0}运行中", (int)status);

// Thread.Sleep(1000);
}
}


public static void ExampleMethodTwo(object status)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("线程{0}", (int)status);

// Thread.Sleep(1000);
}
}
}
}

Timer:

这里指System.Threading.Timer,主要指在多长时间间隔执行方法体。

Timer主要步骤:

① 实例化TimerCallback对象,参数是Timer执行的方法。

② 编写Timer执行的方法。

③ 实例化Timer对象,参数是刚才的TimerCallback对象。

Timer例子:

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

namespace MYTimerTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(timer_Elapsed), null, 0, 1000);
}

void timer_Elapsed(object sender)
{
for (int i = 0; i < 10; i++)
{
Console.Out.WriteLine(DateTime.Now + " " + DateTime.Now.Millisecond.ToString() + "timer in:");
}

}
}
}

事件

事件主要是特定的动作触发而执行方法体。

事件源、监听、触发的方法体三个元素构成。

原文地址:https://www.cnblogs.com/YangBinChina/p/2623119.html