创建定制的泛型类--泛型Queue多线程读写文档--的泛型方法
来源:互联网 发布:淘宝的数字暗语大全 编辑:程序博客网 时间:2024/04/30 09:14
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- namespace Generic_T_Manager
- {
- public interface IDocument
- {
- string Title
- { get;}
- string Content
- { get;}
- }
- public class Document : IDocument
- {
- private string title;
- public string Title
- {
- get { return title; }
- }
- private string content;
- public string Content
- {
- get { return content; }
- }
- public Document(string title, string content)
- {
- this.title = title;
- this.content = content;
- }
- }
- public interface IDocumentManager<T>
- {
- void AddDocument(T doc);
- T GetDocument();
- bool IsDocumentAvailable
- { get;}
- }
- public class DocumentManager<T> : IDocumentManager<T>
- {
- Queue<T> documentQueue = new Queue<T>();
- public void AddDocument(T doc)
- {
- lock (this)
- {
- documentQueue.Enqueue(doc);
- }
- }
- public T GetDocument()
- {
- T doc;
- lock (this)
- {
- doc = documentQueue.Dequeue();
- }
- return doc;
- }
- public bool IsDocumentAvailable
- {
- get { return documentQueue.Count > 0 ? true : false; }
- }
- }
- //where子句要求泛型类实现一个接口,或派生与某个基类。
- //ProcessDocument 独立于Document具体类和DocumentManager具体类
- //是通过定义IDocument和IDocumentManager<T>接口来实现的。
- //ProcessDocument 改为使用两个泛型类TDocument和TDocumentManager。
- //第一个where子句指定TDocument类型必须实现IDocument接口。
- //可以用于TDocument的一种类型是Document具体类,因为这个类实现了IDocument接口。
- //但是,实现了IDocument接口的其它类也可以用于TDocument。
- //TDocumentMananger类型的where子句指定,这个类型必须实现IDocumentManager<TDocument>接口。
- //所以在Document用于TDocument时,TDocumentManager必须是一个实现IDocumentMan<Document>的类。
- public class ProcessDocument<TDocument, TDocumentManager>
- where TDocument:IDocument
- where TDocumentManager:IDocumentManager<TDocument>
- {
- private TDocumentManager documentManager;
- protected ProcessDocument(TDocumentManager dm)
- {
- documentManager = dm;
- }
- protected void Run()
- {
- while (true)
- {
- if (documentManager.IsDocumentAvailable)
- {
- TDocument doc = documentManager.GetDocument();
- Console.WriteLine("Processing document {0}", doc.Title);
- }
- else
- break;
- Thread.Sleep(20);
- }
- }
- public static void Start(TDocumentManager dm)
- {
- new Thread(new ProcessDocument<TDocument, TDocumentManager>(dm).Run).Start();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- DocumentManager<Document> dm = new DocumentManager<Document>();
- ProcessDocument<Document, DocumentManager<Document>>.Start(dm);
- for (int i = 0; i < 200; i++)
- {
- Document d1 = new Document("title" + i.ToString(), "content");
- dm.AddDocument(d1);
- Console.WriteLine("added docuemnt {0}", d1.Title);
- Thread.Sleep(10);
- }
- }
- }
- }
- 创建定制的泛型类--泛型Queue多线程读写文档--的泛型方法
- 泛型Queue多线程读写文档
- Python 多线程同步的另一种方法 Queue
- 多线程的创建方法
- QextSerialPort类多线程读写串口的方法
- android创建多线程的方法
- 创建定制的组件
- 创建定制的ViewGroup
- 文档的读写-单文档
- ios多线程queue的差异
- python Queue的多线程应用
- 用J2SE 5.0创建定制的泛型集合
- XML文档的读写
- vc 文档的读写
- Xml文档的读写
- 文档的读写
- queue的方法
- JQuery的queue方法
- 21.5 UpdatePanelAnimation局部刷新动画控件(征服ASP.NET 2.0 Ajax——Web开发技术详解)
- JGAP使用说明
- 21.6 小结(征服ASP.NET 2.0 Ajax——Web开发技术详解 )
- Posix多线程编程学习笔记(二)—线程属性(1)
- test
- 创建定制的泛型类--泛型Queue多线程读写文档--的泛型方法
- 提高oracle表分析速度
- 输出硬盘文件,提供下载
- Posix多线程编程学习笔记(二)—线程属性(2)
- C64X DSP EDMA/QDMA小结
- PortHidDemo_Vista
- WinCVS 与 SourceForge.net 进行ssh连接
- GIS开源软件推荐-UDIG
- 编译原理资料