zoukankan      html  css  js  c++  java
  • Thread LifeCycle in .net

    At any time, a thread is said to be in one of several thread states (illustrated in Fig. 12.1)2. This section discusses these states and the transitions between states. Two classes critical for multithreaded applications are Threadand Monitor (System.Threadingnamespace). This section also discusses several methods of classes Thread and Monitor that cause state transitions.

    A new thread begins its lifecyle in the Unstarted state. The thread remains in the Unstarted state until the program calls Thread method Start, which places the thread in the Started state (sometimes called the Ready or Runnable state) and immediately returns control to the calling thread. Then the thread that invoked Start, the newly Started thread and any other threads in the program execute concurrently.

    The highest priority Started thread enters the Running state (i.e., begins executing) when the operating system assigns a processor to the thread (Section 12.3 discusses thread priorities). When a Started thread receives a processor for the first time and becomes a 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 the Thread's ThreadStart delegate as the argument to the Thread constructor. The ThreadStart delegate must be a method that returns void and takes no arguments.

    Running thread enters the Stopped (or Dead) state when its ThreadStart delegate terminates. Note that a program can force a thread into the Stopped state by calling Thread method Abort on the appropriate Thread object. Method Abort throws a ThreadAbortException in the thread, normally causing the thread to terminate. When a thread is in the Stopped state 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 to 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.

    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 enters the WaitSleepJoin state by calling Monitor method Wait or by callingThread method Sleep also leaves the WaitSleepJoin state and returns to the Started state if the sleeping or waiting Thread's Interrupt method 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 calls the other thread's Join method to "join" the two threads. When two threads are "joined," the dependent thread leaves the WaitSleepJoin state when the other thread finishes execution (enters the Stopped state). If a Running Thread's Suspend method is called, the Running thread enters the Suspended state. A Suspended thread returns to theStarted state when another thread in the program invokes the Suspended thread's Resume method. 

    ok,Let 's see the diagram of 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 
  • 相关阅读:
    9.12 其他样式;JS
    9.11 样式表
    9.9 样式表
    PHP延迟几秒后执行,PHP延迟执行,PHP停顿几毫秒后执行 usleep() 和 sleep()
    PHP 去除url参数中出现+、空格、=、%、&、#等字符的解决办法, PHP去除影响url路径的字符
    java第一堂课-常用的电脑快捷键操作和重要的dos命令
    常用第三方接口,支付宝支付,微信支付,软著,IOS开发者账号
    苹果开发者账号申请流程完整版 https://www.jianshu.com/p/655380201685
    thinkphp 去重查询
    将数据导【入】【excel表】格式 前端页面
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2531356.html
Copyright © 2011-2022 走看看