G
Guest
Hi everyone!
In program I wrote, I try to run 300 threads, when only 10 is running in the
same time. I use array to create 300 threads, but first I start only 10,
after that I add one thread when some of them is finished. The first 10 are
running well, but when I add one, I get the HashCode that different from 11,
and the all next thread behave in the same way. Any suggestions, why it is
happen?
using System;
using System.Threading;
namespace ThreadTest
{
public class Test
{
public static int iCount = 0;
public static int iMaxCount = 0;
public int iQueueLenth;
public static int iResetPoint;
public ManualResetEvent eventX;
public Test(int MaxCount, int QueueLenth)
{
iMaxCount = MaxCount;
iQueueLenth = QueueLenth;
iResetPoint = QueueLenth - 1;
}
public void UserThread()
{
/// Do something
Console.WriteLine(Thread.CurrentThread.GetHashCode());
Interlocked.Increment(ref iCount);
Interlocked.Decrement(ref iQueueLenth);
if ((iQueueLenth == iResetPoint && (iQueueLenth + iCount) <
iMaxCount) || iCount == iMaxCount)
{
Console.WriteLine();
Console.WriteLine("Setting eventX ");
eventX.Set();
}
}
}
class ThreadTest
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ManualResetEvent eventX = new ManualResetEvent(false); // event to know
when to begin a new thread
int maxCount = 300; // total threads to run
int maxThreadInTime = 10; // num of threads running in the same time
int lastThreadStarted = maxThreadInTime - 1;
Test ot = new Test(maxCount, maxThreadInTime);
ot.eventX = eventX;
Thread[] myThread = new Thread[maxCount];
for (int i=0; i < maxCount; i++)
{
myThread = new Thread(new ThreadStart(ot.UserThread));
}
// Starting the first maxThreadInTime threads
for (int i=0; i < maxThreadInTime; i++)
{
myThread.Start();
}
eventX.WaitOne(Timeout.Infinite,true);
while (lastThreadStarted < (maxCount - 1))
{
Interlocked.Increment(ref lastThreadStarted);
myThread[lastThreadStarted].Start();
Interlocked.Increment(ref ot.iQueueLenth);
eventX.Reset();
eventX.WaitOne(Timeout.Infinite,true);
}
Console.WriteLine ("OK");
}
}
}
In program I wrote, I try to run 300 threads, when only 10 is running in the
same time. I use array to create 300 threads, but first I start only 10,
after that I add one thread when some of them is finished. The first 10 are
running well, but when I add one, I get the HashCode that different from 11,
and the all next thread behave in the same way. Any suggestions, why it is
happen?
using System;
using System.Threading;
namespace ThreadTest
{
public class Test
{
public static int iCount = 0;
public static int iMaxCount = 0;
public int iQueueLenth;
public static int iResetPoint;
public ManualResetEvent eventX;
public Test(int MaxCount, int QueueLenth)
{
iMaxCount = MaxCount;
iQueueLenth = QueueLenth;
iResetPoint = QueueLenth - 1;
}
public void UserThread()
{
/// Do something
Console.WriteLine(Thread.CurrentThread.GetHashCode());
Interlocked.Increment(ref iCount);
Interlocked.Decrement(ref iQueueLenth);
if ((iQueueLenth == iResetPoint && (iQueueLenth + iCount) <
iMaxCount) || iCount == iMaxCount)
{
Console.WriteLine();
Console.WriteLine("Setting eventX ");
eventX.Set();
}
}
}
class ThreadTest
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ManualResetEvent eventX = new ManualResetEvent(false); // event to know
when to begin a new thread
int maxCount = 300; // total threads to run
int maxThreadInTime = 10; // num of threads running in the same time
int lastThreadStarted = maxThreadInTime - 1;
Test ot = new Test(maxCount, maxThreadInTime);
ot.eventX = eventX;
Thread[] myThread = new Thread[maxCount];
for (int i=0; i < maxCount; i++)
{
myThread = new Thread(new ThreadStart(ot.UserThread));
}
// Starting the first maxThreadInTime threads
for (int i=0; i < maxThreadInTime; i++)
{
myThread.Start();
}
eventX.WaitOne(Timeout.Infinite,true);
while (lastThreadStarted < (maxCount - 1))
{
Interlocked.Increment(ref lastThreadStarted);
myThread[lastThreadStarted].Start();
Interlocked.Increment(ref ot.iQueueLenth);
eventX.Reset();
eventX.WaitOne(Timeout.Infinite,true);
}
Console.WriteLine ("OK");
}
}
}