Multithreading in Array

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");

}

}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Yuliaan,

Why would you expect that the hashcode would be the same? If it is a
different thread, and a different object representation, then the hash code
will be different.

What are you trying to do? Also, why are you not using the ThreadPool
class?

Hope this helps.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

GetHashCode () is dependand of the instance, not on the instance type,
that's way it's different for all the instances, why would you think it will
be the same?

why are you creating that many threads?

Cheers,
 
N

Nicholas Paldino [.NET/C# MVP]

It should be notied that this changes somewhat in .NET 2.0. GetHashCode
for the Thread class is not dependent on the instance, but rather, on the
actual concept of the managed thread. Presumably, this was done for
environments (such as SQL Server) where the implementation might be
something besides a physical thread.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

GetHashCode () is dependand of the instance, not on the instance type,
that's way it's different for all the instances, why would you think it will
be the same?

why are you creating that many threads?

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Yuliaan said:
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");

}

}
}

 
G

Guest

No, I don't expect that the hashcode would be the same, but I expect
that It would change in progressive way: 1, 2, 3 ... Until 10 it is realy so
but after that I get 36, 37 ,... 60,...
I don't think that ThreadPool resolve such problem, because I need that in
every moment there will be only 10 threads running (teel the number of thread
have run reach 300).

Nicholas Paldino said:
Yuliaan,

Why would you expect that the hashcode would be the same? If it is a
different thread, and a different object representation, then the hash code
will be different.

What are you trying to do? Also, why are you not using the ThreadPool
class?

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Yuliaan said:
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");

}

}
}

 
N

Nicholas Paldino [.NET/C# MVP]

Yuliaan,

You can not depend on the result of GetHashCode to return values in an
orderly manner. As long as it is unique (as defined by the type), it is
free to return any value that it wants.

Also, if you only want 10 threads running at the same time, using the
thread pool can handle this. Basically, you would have a controller, which
you would query to see if your thread can run. If it can (you don't have
more than ten threads running), then you run the task, otherwise, you
return, and indicate that you want another task to be run.

Either that, or have some sort of notification in the thread processing
to indicate to the controller when it has completed.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Yuliaan said:
No, I don't expect that the hashcode would be the same, but I expect
that It would change in progressive way: 1, 2, 3 ... Until 10 it is realy so
but after that I get 36, 37 ,... 60,...
I don't think that ThreadPool resolve such problem, because I need that in
every moment there will be only 10 threads running (teel the number of thread
have run reach 300).

Nicholas Paldino said:
Yuliaan,

Why would you expect that the hashcode would be the same? If it is a
different thread, and a different object representation, then the hash code
will be different.

What are you trying to do? Also, why are you not using the ThreadPool
class?

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Yuliaan said:
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");

}

}
}

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top