Thread lifecycle in. net

Source: Internet
Author: User

at any time, a thread is said to be in one of several thread States (fig. (12.1) 2. this section discusses these States and the transitions between States. two classes critical for Multithreaded Applications are thread and Monitor (< strong> system. threading namespace ). this section also discusses several methods of classes thread and Monitor that cause state transitions.

A new thread begins its lifecyle inUnstartedState.The thread remains inUnstartedState until the program CILSThreadMethodStart, Which places the thread inStartedState (sometimes calledReadyOrRunnableState) and immediately returns control to the calling thread. Then the thread that invokedStart, The newlyStartedThread and any other threads in the program execute concurrently.

The highest priority Started Thread enters Running State (I. e., begins executing) when the operating system assigns a processor to the thread (section 12.3 discusses thread priorities). When Started Thread es a processor for the first time and becomes Running Thread, the thread executes its Threadstart Delegate, which specifies the actions the thread will perform during its lifecyle. When a program creates a new Thread , The Program specifies Thread 'S Threadstart Delegate as the argument to Thread Constructor. Threadstart Delegate must be a method that returnsVoid And takes no arguments.

ARunningThread entersStopped(OrDead) State when itsThreadstartDelegate terminates. Note that a program can force a thread intoStoppedState by callingThreadMethodAbortOn the appropriateThreadObject. MethodAbortThrowsThreadabortexceptionIn the thread, normally causing the thread to terminate. When a thread is inStoppedState and there are no references to the thread object, the garbage collector can remove the thread object from memory.

A thread enters the blocked state when the thread issues an input/output request. the operating system blocks the thread from executing until the operating system can complete the I/O for which the thread is waiting. at that point, the thread returns to the started state, so it can resume execution. A blocked thread cannot use a processor even if one is available.

there are three ways in which a running thread enters the waitsleepjoin state. if a thread encounters code that it cannot execute yet (normally because a condition is not satisfied ), the thread can call Monitor method wait enter the waitsleepjoin state. once in this state, A thread returns to the started state when another thread invokes Monitor method pulse or pulseall . method pulse moves the next waiting thread back to the started state. method pulseall moves all waiting threads back to the started state.

A running thread can call thread method sleep to enter the waitsleepjoin State for a period of milliseconds specified as the argument to sleep . A sleeping thread returns to the started State when its designated sleep time expires. sleeping threads cannot use a processor, even if one is available.

Any thread that entersWaitsleepjoinState by callingMonitorMethodWaitOr by callingThreadMethodSleepAlso leavesWaitsleepjoinState and returns toStartedState if the sleeping or waitingThread'SInterruptMethod is called by another thread in the program.

If a thread cannot continue executing (we will call this the dependent thread) unless another thread terminates, the dependent thread callthe other thread's Join Method To "join" the two threads. When two threads are "joined," the dependent thread leaves Waitsleepjoin State when the other thread finishes execution (enters Stopped State). If Running Thread 'S Suspend Method is called, Running Thread enters Sushortded State. Sushortded Thread returns to Started State when another thread in the program invokes the suspended thread's Resume Method.

OK, let's see the distriof thread liftcycle

 

See also

Http://www.codeproject.com/Articles/26675/Beginner-s-Guide-to-Threading-in-NET-Part-2-of-n

Http://www.simple-talk.com/dotnet/.net-framework/when-to-run-and-when-to-block/

Http://msdn.microsoft.com/en-us/library/system.threading.threadstate.aspx

 

(PERF testing tools: ants Profiler)

Ants profiler find perf bottleneck example

Http://theniceweb.com/archives/category/ants-profilers

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.