线程、线程池、Timer、事件的理解与区别
2012-08-04 17:19
281 查看
首先明确什么时候用多线程?
多线程是提高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:");
}
}
}
}
事件
事件主要是特定的动作触发而执行方法体。
事件源、监听、触发的方法体三个元素构成。
多线程是提高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:");
}
}
}
}
事件
事件主要是特定的动作触发而执行方法体。
事件源、监听、触发的方法体三个元素构成。
相关文章推荐
- 线程与进程的区别以及对多线程并发的理解
- 进程与线程的区别,线程安全”怎么理解?(2012.5.6百度实习生笔试)
- 在线程池使用Callable和Runnable的区别以及如何关闭线程
- 理解button的Click事件和TextBox的TextChanged\DropDownList.SelectedIndexChanged的实现原理的区别
- 进程与线程的区别,线程安全”怎么理解?(2012.5.6百度实习生笔试)
- javascript中的事件捕获机制,深入理解并区别IE和DOM中的事件模型
- STM32的"异常“、“中断”和“事件”区别和理解
- 理解进程,线程的区别
- Qt4.7中,线程,信号,事件的一点理解
- 关于线程中sleep()与wait()区别的理解
- 快速理解C#高级概念(二) 事件与委托的区别
- ThreadPoolExecutor线程池和Thrad方式的理解和区别
- 理解Java与Android中线程与线程池的原理和使用
- 一个故事帮你理解线程和线程池
- STM32 外部中断和事件 的区别和理解
- Thread--01在线程池使用Callable和Runnable的区别以及如何关闭线程
- 同步事件,信号量,互斥,临界区,线程,线程池C++实现(win32,linux)
- 快速理解C#高级概念(二) 事件与委托的区别
- C# 线程中使用Timer,并向其触发的事件(函数)中传递参数
- 快速理解C#高级概念事件与委托的区别