Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when parent thread sleep does sub threads also sleep?

when parent thread sleep does sub threads also sleep ?

Now main thread is UI I create 20 sub threads inside main thread with task factory (lets call threads 2) Inside of this 20 sub threads i create another 10 sub threads again with sub factory (lets call threads 3)

Now inside of this threads 2 i have infinite loop. Inside of infinite loop checking whether threads 3 completed or not. If completed dispose completed thread and start another thread. I am using 250 ms sleep for each checking inside infinite while loop. So when threads 2 in sleep does also threads 3 sleep or they are independent. Here the code you can see.

while (true)
{
    int irActiveThreadCount = 0;
    int irFinishedLast = -1;

    for (int i = 0; i < irPerMainSiteThreadCount; i++)
    {
        if (MainSitesTaskList[irWhichMainTask, i] == null)
        {
            irFinishedLast = i;
            break;
        }
        if (MainSitesTaskList[irWhichMainTask, i].IsCompleted == true)
        {
            irFinishedLast = i;
            break;
        }
    }

    for (int i = 0; i < irPerMainSiteThreadCount; i++)
    {
        if (MainSitesTaskList[irWhichMainTask, i] != null)
            if (MainSitesTaskList[irWhichMainTask, i].IsCompleted == false)
            {
                irActiveThreadCount++;
            }
    }

    if (irFinishedLast > -1)
    {
        var newTask = Task.Factory.StartNew(() =>
        {
            fcStartSubPageCrawl(srMainSiteURL, srMainSiteId, irWhichMainTask);
        });
        lock (lockerMainSitesArray)
        {
            if (MainSitesTaskList[irWhichMainTask, irFinishedLast] != null)
                MainSitesTaskList[irWhichMainTask, irFinishedLast].Dispose();
            MainSitesTaskList[irWhichMainTask, irFinishedLast] = newTask;
        }
    }

    Thread.Sleep(250);

    srQuery = "myquery";
    using (DataSet dsTemp = DbConnection.db_Select_Query(srQuery))
    {
        if (dsTemp != null)
            if (dsTemp.Tables.Count > 0)
                if (dsTemp.Tables[0].Rows.Count == 0)
                {
                    break;
                }
    }
}
like image 527
MonsterMMORPG Avatar asked Oct 22 '11 13:10

MonsterMMORPG


People also ask

Does thread sleep block other threads?

Sleep method causes the current thread to immediately block for the number of milliseconds or the time interval you pass to the method, and yields the remainder of its time slice to another thread. Once that interval elapses, the sleeping thread resumes execution. One thread cannot call Thread. Sleep on another thread.

What happens to a thread when it sleeps?

Thread. sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.

Does thread sleep block the main thread?

sleep is blocking. We now understand that we cannot use Thread. sleep – it blocks the thread.

Do threads have parent/child relationship?

No. There is no copy of parent stack which is passed on to child.


3 Answers

There's no such thing as a "parent" thread really. One thread starts another, but then there's no particular relationship between them. For example, the starting thread can terminate without any of the new threads dying.

The starting thread sleeping definitely doesn't affect any other thread.

like image 166
Jon Skeet Avatar answered Oct 23 '22 01:10

Jon Skeet


There is no concept of parent and child threads. One implication of this is that the child threads don't sleep when the parent thread sleeps.

like image 1
John Saunders Avatar answered Oct 23 '22 01:10

John Saunders


Thread.Sleep(...)

only suspends the current Thread.

check here: Thread.Sleep Method

so all other threads will keep working.

like image 1
Davide Piras Avatar answered Oct 23 '22 03:10

Davide Piras