When is the sleeping thread of the parent thread also a dream?

when is the sleeping thread of the parent thread also sleeping?

Now the main thread is the user interface. I create 20 subpages inside the main thread with the factory task (allows you to call threads 2) Inside these 20 subtypes, I again create 10 more subtopics with the sub factory (allows you to call threads 3)

Now inside these threads 2 I have an infinite loop. Inside an infinite loop, it checks to see if threads 3 are complete or not. If completed, delete the completed thread and start another thread. I use a 250 ms sleep for every check inside an infinite loop. Therefore, when threads 2 in a dream, threads 3 also do not work or they are independent. Here is 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; } } } 
+6
source share
5 answers

There is no such thing as a "parent" thread. One thread begins with another, but then there is no special relationship between them. For example, the initial thread may terminate without any new threads.

The original sleep thread definitely does not affect any other thread.

+10
source

There is no concept of parent and child streams. One consequence of this is that child threads are not sleeping when the parent thread is sleeping.

+1
source
 Thread.Sleep(...) 

only pauses the current thread.

here: Thread.Sleep Method

so that all other threads continue to work.

+1
source

Each thread is always completely independent. The only possible connection between threads is that when the non-phonic thread completes, the program ends, so the background threads die. If the thread sleeps, other threads continue to work (and probably go faster, because there is another work with the thread). If you need to synchronize threads, there are various classes for this (in general, locks (not a class), mutexes, semaphores ...)

0
source

Others are right, there is no concept of "parent threads" in .Net. And waiting in one thread does not cause other threads to wait (unless some synchronization is used, for example, using lock s).

But there is another point: your code does not create new threads, at least not necessarily. When you call Task.Factory.StartNew() , the task is usually assigned in the thread pool thread. If there are no threads and the number of threads has not reached the maximum number, a new thread is created. But in other cases this is not so. The challenge is to either reuse an existing idle thread or wait until it becomes available.

0
source

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


All Articles