Thread.Join vs Thread.State

Thread.Join returns us if the thread is completed. The same can be determined using ThreadState. Then what is the difference between Thread.Join () and Thread.ThreadState?

Can they be used interchangeably?

+7
multithreading c #
source share
4 answers

The difference between connecting and manually accessing ThreadState is that Join is a blocking operation. The function will not return until the timeout is reached, or the Thread target completes. The ThreadState check ThreadState more like a spy operation.

+14
source share

Thread.join WAITING to complete the thread. ThreadState just gives you a snapshot of the thread and returns without waiting. There is also a Thread.join option that takes time to wait. ThreadState and Join are very different, and I do not think that both of them can be interchanged.

Try a test where you make both calls to a thread that has an infinite loop.

+6
source share

When you call Thread.Join() , it blocks the calling thread until the thread having the Join method terminates. If it is interrupted or successfully completed, the connection () that follows will not block the calling thread. This allows you to have 10 worker threads and one main thread that should be executed after all 10 threads have completed. That way you can call Join () on the first thread. This call blocks the main thread until the first worker thread completes. After that, you can call Join () in the second thread, and so on, until you get to thread # 10. When you call Join () on it, and the main thread resumes, you can be sure that all 10 threads are completed, and the main thread can resume its work.

For example:

 Thread workers[] = new Thread[10]; //*** create and start threads *** foreach(Thread worker in workers) { worker.Join(); } //All threads are completed, now this operation can continue... 

On the other hand, Thread.ThreadState returns the status of the thread (Aborted, Running, ...) without affecting the calling thread state (not like Join, which puts the calling thread in WaitSleepJoin state). So this should be used only if you want to check what happens to the stream so that you can take a specific action or want to implement your own Join mechanism, etc.

+3
source share

Thread.Join will block the calling thread

0
source share

All Articles