异步编程(1)
来源:互联网 发布:211xf域名升级访问 编辑:程序博客网 时间:2024/04/30 00:58
我们知道,使用多线程可以提高程序运行的效率,加速程序的运行。但是我们也应该知道每个线程都要耗费许多资源,在程序中不是运行的线程越多好,我们要掌握如何充分利用多线程的优势。要尽量使线程运行,不要让它挂起,因为挂起的线程不再运行。但是仍然耗费系统资源。
线程池是一种非常好的技术,可以大大提高程序的效率,而且又把新建每个线程的消耗降到最小。下面我们看一下c#中提供的有关线程池的方法以及如何使用:
static Boolean QueueUserWorkItem(WaitCallback callBack);
static Boolean QueueUserWorkItem(WaitCallback callBack, Object state);
static Boolean UnsafeQueueUserWorkItem(WaitCallback callBack, Object state);
using System;
using System.Threading;
public static class Program ...{
public static void Main() ...{
Console.WriteLine("Main thread: queuing an asynchronous operation");
ThreadPool.QueueUserWorkItem(ComputeBoundOp, 5);
Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(10000);
Console.WriteLine("Hit <Enter> to end this program...");
Console.ReadLine();
}
private static void ComputeBoundOp(Object state) ...{
Console.WriteLine("In ComputeBoundOp: state={0}", state);
Thread.Sleep(1000);
}
}
//有时候是这样的结果
Main thread: queuing an asynchronous operation
Main thread: Doing other work here...
In ComputeBoundOp: state=5
//有时候又是这样的结果
Main thread: queuing an asynchronous operation
In ComputeBoundOp: state=5
Main thread: Doing other work here...
//我们看出两个线程的调度是由系统决定的,我们是无法选择的。
static Boolean QueueUserWorkItem(WaitCallback callBack, Object state);
static Boolean UnsafeQueueUserWorkItem(WaitCallback callBack, Object state);
using System;
using System.Threading;
public static class Program ...{
public static void Main() ...{
Console.WriteLine("Main thread: queuing an asynchronous operation");
ThreadPool.QueueUserWorkItem(ComputeBoundOp, 5);
Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(10000);
Console.WriteLine("Hit <Enter> to end this program...");
Console.ReadLine();
}
private static void ComputeBoundOp(Object state) ...{
Console.WriteLine("In ComputeBoundOp: state={0}", state);
Thread.Sleep(1000);
}
}
//有时候是这样的结果
Main thread: queuing an asynchronous operation
Main thread: Doing other work here...
In ComputeBoundOp: state=5
//有时候又是这样的结果
Main thread: queuing an asynchronous operation
In ComputeBoundOp: state=5
Main thread: Doing other work here...
//我们看出两个线程的调度是由系统决定的,我们是无法选择的。
下面再来看一下使用专用线程,我们知道如果程序只需要其他的1~2线程来辅助程序运行,这种情况下用线程池恐怕不是很适合,而使用专用线程比较好:
using System;
using System.Threading;
public static class Program ...{
public static void Main() ...{
Console.WriteLine("Main thread: starting a dedicated thread " +"to do an asynchronous operation");
Thread dedicatedThread = new Thread(ComputeBoundOp);
dedicatedThread.Start(5);
Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(10000);
dedicatedThread.Join(); //等待线程终止
Console.WriteLine("Hit <Enter> to end this program...");
Console.ReadLine();
}
private static void ComputeBoundOp(Object state) ...{
Console.WriteLine("In ComputeBoundOp: state={0}", state);
Thread.Sleep(1000);
}
}
//同样是两种输出结果
Main thread: starting a dedicated thread to do an asynchronous operation
Main thread: Doing other work here...
In ComputeBoundOp: state=5
Main thread: starting a dedicated thread to do an asynchronous operation
In ComputeBoundOp: state=5
Main thread: Doing other work here...
using System.Threading;
public static class Program ...{
public static void Main() ...{
Console.WriteLine("Main thread: starting a dedicated thread " +"to do an asynchronous operation");
Thread dedicatedThread = new Thread(ComputeBoundOp);
dedicatedThread.Start(5);
Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(10000);
dedicatedThread.Join(); //等待线程终止
Console.WriteLine("Hit <Enter> to end this program...");
Console.ReadLine();
}
private static void ComputeBoundOp(Object state) ...{
Console.WriteLine("In ComputeBoundOp: state={0}", state);
Thread.Sleep(1000);
}
}
//同样是两种输出结果
Main thread: starting a dedicated thread to do an asynchronous operation
Main thread: Doing other work here...
In ComputeBoundOp: state=5
Main thread: starting a dedicated thread to do an asynchronous operation
In ComputeBoundOp: state=5
Main thread: Doing other work here...
下面看一下定期执行某种操作我们应该如何使用定时器:
using System;
using System.Threading;
public static class Program ...{
public static void Main() ...{
Console.WriteLine("Main thread: starting a timer");
Timer t = new Timer(ComputeBoundOp, 5, 0, 2000);
Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(10000);
t.Dispose();
}
private static void ComputeBoundOp(Object state) ...{
Console.WriteLine("In ComputeBoundOp: state={0}", state);
Thread.Sleep(1000); // Simulates other work (1 second)
}
}
using System.Threading;
public static class Program ...{
public static void Main() ...{
Console.WriteLine("Main thread: starting a timer");
Timer t = new Timer(ComputeBoundOp, 5, 0, 2000);
Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(10000);
t.Dispose();
}
private static void ComputeBoundOp(Object state) ...{
Console.WriteLine("In ComputeBoundOp: state={0}", state);
Thread.Sleep(1000); // Simulates other work (1 second)
}
}
如果我们查看FCL的话,我们会看到三个Timer类,下面分别介绍一下他们:
System.Threading.Timer //这个定时器就是我们使用的那个,是最好的一个定时器,建议都使用这个
System.Windows.Forms.Timer
/**//*构建一个该类的实例可以告诉Windows将定时器与调用线程关联。随着定时器的触发,Windows将一个定时消息插入到线程的消息队列中。调用线程必须执行一个消息循环,从而提取消息,并分别将他们分派到希望的回调方法中。*/
System.Timers.Timer
/**//*该定时器基本上是对System.Threading.Timer 的一个封装,当定时器到期后,经导致FCL将事件加入线程池队列中。这个定时器有可能被移除,以便都使用System.Threading.Timer */
System.Windows.Forms.Timer
/**//*构建一个该类的实例可以告诉Windows将定时器与调用线程关联。随着定时器的触发,Windows将一个定时消息插入到线程的消息队列中。调用线程必须执行一个消息循环,从而提取消息,并分别将他们分派到希望的回调方法中。*/
System.Timers.Timer
/**//*该定时器基本上是对System.Threading.Timer 的一个封装,当定时器到期后,经导致FCL将事件加入线程池队列中。这个定时器有可能被移除,以便都使用System.Threading.Timer */
- 异步编程(1)
- 异步编程(1)
- asyncjs介绍--异步编程风格(1)
- 异步编程篇1
- 异步编程简介(1)
- 使用.NET 异步编程(1)
- 使用.NET 异步编程(1)
- C# 异步编程篇1
- Node.js[1] 异步编程
- Python异步编程1-协程
- 异步编程(Asynchronous Programming)
- javascript异步编程(一)
- 异步编程系列(一)
- C#编程总结(六)异步编程
- C#编程总结(六)异步编程
- C#编程总结(六)异步编程
- C#编程总结(六)异步编程
- 第十章(1)-异步编程模式-学习笔记
- 面向Web的数据挖掘
- 谁敢炸三峡?美国步步进逼
- 北京派出所网点
- 关于.NET FrameWork3.0的一些知识
- Web数据挖掘技术综述
- 异步编程(1)
- 北京旧货市场地址
- Linux小技巧收集
- OutOfMemory PermGen引起的tomcat停止服务
- 北京社保查询
- Web数据挖掘
- 用Marathon管理Firebird数据库(2)
- 如何将*.style符号库文件转成*.serversytle符号库文件
- 北京邮政局营业网点