Restarting regular threads

Let's say what I want to do to check a million lines, and each check is done in a couple of seconds.

My approach:

I have an array of threads declared as follows:

Thread[] workers = new Thread[50];

I don’t have all the lines in the array, they go through some calculations, then I don’t have them when I start this process, but I have a method that returns the following:

public string next()
{
  //my code
}

I managed to start all 50 threads, like this:

for (int x = 0; x < 50; x++)
{
workers[x] = new Thread(new ParameterizedThreadStart(myMethod));
workers[x].Start(next());
}

which quickly starts all 50 threads "at the same time" and then my log (submitted by myMethod) receives 50 responses almost at the same time (1 ~ 1.5 seconds)

How to get every thread that has just completed to execute it again with the next line, given that the Thread class does not show any event or something like that?

. , Threads, BackgroundWorkers.

# .net 3.5.

+3
4

, ThreadPool. :

while(MoreWorkIsAvailable)
{
    string nextString = next();
    ThreadPool.QueueUserWorkItem(new WaitCallback(myMethod), nextString);
}

, SetMaxThreads.

+5

. Thread.Join, , . - while , , .

+2

next() , ADO.NET . , , null while, null, .

, . next() , . , next(). - - , , (), :

, , .

: , ThreadPool . :

YourStringGenerator generator;
//instatiate generator
for (int x = 0; x < 50; x++) 
{ 
    workers[x] = new Thread(new ParameterizedThreadStart(myMethod)); 
    workers[x].Start(generator); 
}

myMethod(YourStringGenerator generator)
{
    String compare;
    while((compare=generator.next())!=null)
    {
        //do comparison, etc.
    }
    return;
}

next() :

String next()
{
    lock(this.index)  //see msdn for info.  Link below.
    {
        //determine next string
        //update index
    }
    //generate or get next string from list and return it
    //or if empty, return null
}

. msdn

+1

, , " ".

MoveNext Current. . , , .

Microsoft Px (?). , ( db), - .

+1

Source: https://habr.com/ru/post/1768681/


All Articles