趣味小问题,感兴趣的进N(假如10)个职员在屋里,每个人每分钟都要向领导交1块钱,最长时间M(假如20)分钟也要交的,如果过了10分钟了,有人还没交,领导就要催促那些没交的人,让他们交钱.程序模拟实现[解决办法]又是观察者?[解决办法]领导为观察者,每个员工注册到领导的观察列表中,到时间后,领导从列表中选择出没交钱的,依次发送通知[解决办法]这其实是一个优先级队列。[解决办法]
项目中遇到的问题:M个监控项,每个监控项根据自己的调度计划,每隔一定时间返回一个值,如果N个小时内,某个监控项的返回值都一样,那么找出这样的监控项,做进一步处理。
using System;using System.Collections.Generic;using System.Threading;namespace WindowsFormsApplication4{ /// <summary> /// 领导 /// </summary> public class Leader : IDisposable { #region 成员变量 /// <summary> /// 员工列表 /// </summary> private IDictionary<String, Employee> _EmployeeList; /// <summary> /// 服务状态 /// </summary> private Boolean _Active; /// <summary> /// 超时时间 /// </summary> private readonly Int64 _TimeOutTick; #endregion #region 私有方法 /// <summary> /// 1秒轮询一次,找出没交保护费的员工 /// </summary> /// <param name="obj"></param> private void Polling(Object obj) { while (_Active) { try { foreach (String key in _EmployeeList.Keys) { Employee item = _EmployeeList[key]; if ((DateTime.Now.Ticks - item.LastActiveTime) > _TimeOutTick) { item.Urged(); } } } finally { Thread.Sleep(1000); } } } #endregion #region 公有方法 /// <summary> /// 构造函数 /// </summary> public Leader() { // 10秒断线 _TimeOutTick = 10 * 10000000; _Active = false; } /// <summary> /// 设置员工列表 /// </summary> /// <param name="employeeList"></param> public void SetEmployeeList(Dictionary<String, Employee> employeeList) { _EmployeeList = employeeList; } /// <summary> /// 启动 /// </summary> public void Start() { _Active = true; ThreadPool.QueueUserWorkItem(new WaitCallback(Polling), null); } /// <summary> /// 收费 /// </summary> /// <param name="Name"></param> public void Charge(String Name) { try { Employee employee; if (_EmployeeList.TryGetValue(Name, out employee)) { employee.LastActiveTime = System.DateTime.Now.Ticks; } } finally { } } /// <summary> /// 释放资源 /// </summary> public void Dispose() { if (_Active) { _Active = false; _EmployeeList.Clear(); _EmployeeList = null; } } #endregion }}
/// <param name="obj"></param> private void Polling(Object obj) { while (_Active) { try { foreach (String key in _EmployeeList.Keys) { Employee item = _EmployeeList[key]; if ((DateTime.Now.Ticks - item.LastActiveTime) > _TimeOutTick) { item.Urged(); } } } finally { Thread.Sleep(1000); } } } #endregion #region 公有方法 /// <summary> /// 构造函数 /// </summary> public Leader() { // 10秒断线 _TimeOutTick = 10 * 10000000; _Active = false; } /// <summary> /// 设置员工列表 /// </summary> /// <param name="employeeList"></param> public void SetEmployeeList(Dictionary<String, Employee> employeeList)
{ _EmployeeList = employeeList; } /// <summary> /// 启动 /// </summary> public void Start() { _Active = true; ThreadPool.QueueUserWorkItem(new WaitCallback(Polling), null); } /// <summary> /// 收费 /// </summary> /// <param name="Name"></param> public void Charge(String Name) { try { Employee employee; if (_EmployeeList.TryGetValue(Name, out employee)) { employee.LastActiveTime = System.DateTime.Now.Ticks; } } finally { } } /// <summary> /// 释放资源 /// </summary> public void Dispose() { if (_Active) { _Active = false; _EmployeeList.Clear(); _EmployeeList = null; } } #endregion }}
using System;using System.Windows.Forms;namespace WindowsFormsApplication4{ /// <summary> /// 用户信息 /// </summary> public class Employee { /// <summary> /// 姓名 /// </summary> public String Name { get; set; } /// <summary> /// 最后活跃时间 /// </summary> public Int64 LastActiveTime { get; set; } /// <summary> /// 领导 /// </summary> private Leader _Leader; /// <summary> /// 构造函数 /// </summary> /// <param name="name"></param> /// <param name="leader"></param> public Employee(String name, Leader leader) { Name = name; LastActiveTime = DateTime.Now.Ticks; _Leader = leader; } /// <summary> /// 交保护费 /// </summary> public void PayProtectionMoney() { _Leader.Charge(Name); } /// <summary> /// 催交 /// </summary> public void Urged() { MessageBox.Show(String.Format("{0} 该交保护费了,不然~~~~", Name)); } }}
/// </summary> public class Employee { /// <summary> /// 姓名 /// </summary> public String Name { get; set; } /// <summary> /// 最后活跃时间 /// </summary> public Int64 LastActiveTime { get; set; } /// <summary> /// 领导 /// </summary> private Leader _Leader; /// <summary> /// 构造函数 /// </summary> /// <param name="name"></param> /// <param name="leader"></param> public Employee(String name, Leader leader) { Name = name; LastActiveTime = DateTime.Now.Ticks; _Leader = leader; } /// <summary> /// 交保护费 /// </summary> public void PayProtectionMoney() { _Leader.Charge(Name); } /// <summary> /// 催交 /// </summary> public void Urged() { MessageBox.Show(String.Format("{0} 该交保护费了,不然~~~~", Name)); } }}
using System;using System.Collections.Generic;using System.Windows.Forms;namespace WindowsFormsApplication4{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } Dictionary<String, Employee> _EmployeeList = new Dictionary<String, Employee>(); private void button1_Click(object sender, EventArgs e) { Leader leader= new Leader(); String name = String.Empty; for (Int32 i = 0; i < 100;i++) { name = String.Format("employee" + i); Employee employee = new Employee(name, leader); _EmployeeList.Add(name, employee); } leader.SetEmployeeList(_EmployeeList); leader.Start(); } private void button2_Click(object sender, EventArgs e) { _EmployeeList["employee1"].PayProtectionMoney(); } }}
{ public Form1() { InitializeComponent(); } Dictionary<String, Employee> _EmployeeList = new Dictionary<String, Employee>(); private void button1_Click(object sender, EventArgs e) { Leader leader= new Leader(); String name = String.Empty; for (Int32 i = 0; i < 100;i++) { name = String.Format("employee" + i); Employee employee = new Employee(name, leader); _EmployeeList.Add(name, employee); } leader.SetEmployeeList(_EmployeeList); leader.Start(); } private void button2_Click(object sender, EventArgs e) { _EmployeeList["employee1"].PayProtectionMoney(); } }}