public class Document {
public string Title { get; private set; }
public string Content { get; private set; }
public Document(string title, string content)
{
this.Title = title;
this.Content = content;
}
}
public class ProcessDocuments
{
public static void Start(DocumentManager dm)
{
Thread th = new Thread(new ProcessDocuments(dm).Run);
th.Start();
}
protected ProcessDocuments(DocumentManager dm)
{
if (dm == null)
throw new ArgumentNullException("dm");
documentManager = dm;
}
private DocumentManager documentManager;
protected void Run()
{
while(true)
{
if(documentManager.IsDocumentAvailable)
{
Document doc = documentManager.GetDocument();
Log.error("Procession document {0}" + doc.Title);
}
Thread.Sleep(new System.Random().Next(20));
}
}
}
public class DocumentManager
{
private readonly Queue<Document> documentQueue = new Queue<Document>();
public void AddDocument(Document doc)
{
lock (this)
{
documentQueue.Enqueue(doc);
}
}
public Document GetDocument()
{
Document doc = null;
lock (this)
{
doc = documentQueue.Dequeue();
}
return doc;
}
public bool IsDocumentAvailable
{
get
{
return documentQueue.Count > 0;
}
}
}
static void Main()
{
var dm = new DocumentManager();
ProcessDocuments.Start(dm);
for (int i = 0; i < 1000; i++)
{
Document doc = new Document("Doc " + i.ToString(), "content");
dm.AddDocument(doc);
Log.error("Added document {0}" + doc.Title);
//Console.WriteLine("Added document {0}", doc.Title);
Thread.Sleep(new System.Random().Next(20));
}
}
public string Title { get; private set; }
public string Content { get; private set; }
public Document(string title, string content)
{
this.Title = title;
this.Content = content;
}
}
public class ProcessDocuments
{
public static void Start(DocumentManager dm)
{
Thread th = new Thread(new ProcessDocuments(dm).Run);
th.Start();
}
protected ProcessDocuments(DocumentManager dm)
{
if (dm == null)
throw new ArgumentNullException("dm");
documentManager = dm;
}
private DocumentManager documentManager;
protected void Run()
{
while(true)
{
if(documentManager.IsDocumentAvailable)
{
Document doc = documentManager.GetDocument();
Log.error("Procession document {0}" + doc.Title);
}
Thread.Sleep(new System.Random().Next(20));
}
}
}
public class DocumentManager
{
private readonly Queue<Document> documentQueue = new Queue<Document>();
public void AddDocument(Document doc)
{
lock (this)
{
documentQueue.Enqueue(doc);
}
}
public Document GetDocument()
{
Document doc = null;
lock (this)
{
doc = documentQueue.Dequeue();
}
return doc;
}
public bool IsDocumentAvailable
{
get
{
return documentQueue.Count > 0;
}
}
}
static void Main()
{
var dm = new DocumentManager();
ProcessDocuments.Start(dm);
for (int i = 0; i < 1000; i++)
{
Document doc = new Document("Doc " + i.ToString(), "content");
dm.AddDocument(doc);
Log.error("Added document {0}" + doc.Title);
//Console.WriteLine("Added document {0}", doc.Title);
Thread.Sleep(new System.Random().Next(20));
}
}