C#任务队列的实现代码
2012-10-30 10:48
405 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Text;
namespace TaskList
{
public class Tasks
{
private static Queue<Action> m_List;
//线程互斥
private static object m_obj = new object();
/// <summary>
/// 初始化队列
/// </summary>
public Tasks()
{
if(m_List == null)
m_List = new Queue<Action>();
}
/// <summary>
/// 线程工作的函数
/// </summary>
public void ThreadWork()
{
while (true)
{
//获取任务
Action work = Pop();
//执行任务
work();
Thread.Sleep(1);
}
}
/// <summary>
/// 从任务队列中取出任务
/// </summary>
/// <returns></returns>
public Action Pop()
{
Monitor.Enter(m_obj);
Action ac = null;
try
{
//当队列有数据,出队.否则等待
if (m_List.Count > 0)
{
ac = m_List.Dequeue();
}
else
{
Monitor.Wait(m_obj);
ac = m_List.Dequeue();
}
}
finally
{
Monitor.Exit(m_obj);
}
return ac;
}
/// <summary>
/// 把任务加入任务队列
/// </summary>
public void Push()
{
Work w = new Work();
//上锁
Monitor.Enter(m_obj);
//委托
Action action = new Action(w.RunWork);
//把任务加入队列中
m_List.Enqueue(action);
//通知等待队列中的线程锁定对象状态的更改。
Monitor.Pulse(m_obj);
Monitor.Exit(m_obj);
}
}
public class Work
{
private static int number;
/// <summary>
/// 工作函数
/// </summary>
public void RunWork()
{
number++;
Console.WriteLine("hello world:" + number.ToString());
}
}
public class Program
{
public static void Main()
{
//加入任务
for (int i = 0; i < 200; i++)
{
Tasks tast = new Tasks();
tast.Push();
}
//开启线程来完成执行任务队列中的任务
for (int i = 0; i < 2; i++)
{
Tasks t = new Tasks();
Thread th = new Thread(new ThreadStart(t.ThreadWork));
th.Start();
}
Console.Read();
}
}
}
//C#任务队列的简单实现,鄙人经验短浅,欢迎朋友评点指出,O(∩_∩)O谢谢!
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Text;
namespace TaskList
{
public class Tasks
{
private static Queue<Action> m_List;
//线程互斥
private static object m_obj = new object();
/// <summary>
/// 初始化队列
/// </summary>
public Tasks()
{
if(m_List == null)
m_List = new Queue<Action>();
}
/// <summary>
/// 线程工作的函数
/// </summary>
public void ThreadWork()
{
while (true)
{
//获取任务
Action work = Pop();
//执行任务
work();
Thread.Sleep(1);
}
}
/// <summary>
/// 从任务队列中取出任务
/// </summary>
/// <returns></returns>
public Action Pop()
{
Monitor.Enter(m_obj);
Action ac = null;
try
{
//当队列有数据,出队.否则等待
if (m_List.Count > 0)
{
ac = m_List.Dequeue();
}
else
{
Monitor.Wait(m_obj);
ac = m_List.Dequeue();
}
}
finally
{
Monitor.Exit(m_obj);
}
return ac;
}
/// <summary>
/// 把任务加入任务队列
/// </summary>
public void Push()
{
Work w = new Work();
//上锁
Monitor.Enter(m_obj);
//委托
Action action = new Action(w.RunWork);
//把任务加入队列中
m_List.Enqueue(action);
//通知等待队列中的线程锁定对象状态的更改。
Monitor.Pulse(m_obj);
Monitor.Exit(m_obj);
}
}
public class Work
{
private static int number;
/// <summary>
/// 工作函数
/// </summary>
public void RunWork()
{
number++;
Console.WriteLine("hello world:" + number.ToString());
}
}
public class Program
{
public static void Main()
{
//加入任务
for (int i = 0; i < 200; i++)
{
Tasks tast = new Tasks();
tast.Push();
}
//开启线程来完成执行任务队列中的任务
for (int i = 0; i < 2; i++)
{
Tasks t = new Tasks();
Thread th = new Thread(new ThreadStart(t.ThreadWork));
th.Start();
}
Console.Read();
}
}
}
//C#任务队列的简单实现,鄙人经验短浅,欢迎朋友评点指出,O(∩_∩)O谢谢!
相关文章推荐
- C#定时任务采用线程和队列实现
- C#代码实现隐藏任务栏、开始菜单和禁用任务管理
- Java通过缓存队列执行任务代码实现
- Python 30 行代码实现小型多线程任务队列
- C#实现rabbitmq 延迟队列功能实例代码
- C#实现任务队列
- 可替代log4j日志的c#简单日志类队列实现类代码分享
- 在C#代码中实现gridview不自动换行
- 简单实现C#生成XML文件代码
- C#窗体的最大化/最小化/还原/最小化到托盘/NotifyIcon的代码实现
- C#实现软件自动升级代码【编程联盟】
- 使用C#实现顺序队列
- 多线程任务队列MulitiThreadJob实现
- 【Dictionary遍历】C#中Dictionary几种遍历的实现代码
- C#基础系列:实现自己的ORM(MiniORM的测试代码)
- WPF关于C#代码实现Template
- C#一个简单的定时小程序实现代码
- C#实现人民币大写转换示例代码
- C#操作IIS程序池及站点的创建配置实现代码