G
Guest
Hi,
I am in need to a Queue class for a high speed read write operation where i
do not have to look the object / collection in order to add / remove items. I
had a look at System.Collections.Queue class and i need some clarification:
msdn documentation says that instance methods are not thread-safe. However
when i run the spinnet below i do not get any exception not matter how long i
run it for. Any advice on how i should be using the Queue class with calls to
Enqueue and Dequeue would be greatly appreciated
Code Snip:
-----------------------------------------------
using System;
using System.Collections;
using System.Threading;
public class MyClass
{
private static Queue oQueue = null;
private static bool bContinue = true;
public static void Main()
{
oQueue = new Queue();
Thread oThread1 = new Thread(new ThreadStart(WriteQueue));
Thread oThread2 = new Thread(new ThreadStart(ReadQueue));
oThread1.Start();
oThread2.Start();
RL();
}
private static void ReadQueue()
{
while(true)
{
if(oQueue.Count > 0)
{
int nValue = (int)oQueue.Dequeue();
Console.WriteLine("Read from Queue: {0}", nValue);
}
}
}
private static void WriteQueue()
{
for(int i = 1; i < 10000; i++)
{
oQueue.Enqueue(i);
Console.WriteLine("Writing to Queue: {0}", i);
}
bContinue = false;
}
#region Helper methods
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
I am in need to a Queue class for a high speed read write operation where i
do not have to look the object / collection in order to add / remove items. I
had a look at System.Collections.Queue class and i need some clarification:
msdn documentation says that instance methods are not thread-safe. However
when i run the spinnet below i do not get any exception not matter how long i
run it for. Any advice on how i should be using the Queue class with calls to
Enqueue and Dequeue would be greatly appreciated
Code Snip:
-----------------------------------------------
using System;
using System.Collections;
using System.Threading;
public class MyClass
{
private static Queue oQueue = null;
private static bool bContinue = true;
public static void Main()
{
oQueue = new Queue();
Thread oThread1 = new Thread(new ThreadStart(WriteQueue));
Thread oThread2 = new Thread(new ThreadStart(ReadQueue));
oThread1.Start();
oThread2.Start();
RL();
}
private static void ReadQueue()
{
while(true)
{
if(oQueue.Count > 0)
{
int nValue = (int)oQueue.Dequeue();
Console.WriteLine("Read from Queue: {0}", nValue);
}
}
}
private static void WriteQueue()
{
for(int i = 1; i < 10000; i++)
{
oQueue.Enqueue(i);
Console.WriteLine("Writing to Queue: {0}", i);
}
bContinue = false;
}
#region Helper methods
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}