Thread lifecycle in .NET framework

六眼飞鱼酱① 提交于 2019-12-22 01:38:01

问题


The state of a thread in .NET framework is explained in this link.

I recently saw this picture in a web-site and a couple of questions came to my mind:

  1. The thread lifecycle in the OS is not completely aligned with thread lifecycle in .NET framework. Can someone provide a resource that matches the states in OS with .NET framework?

  2. We don't have a state called Blocked in .NET framework. What will be the state of a thread if it issues an I/O request?

  3. What is the purpose of the Aborted state? When a thread calls the Abort() method, it will go to the AbortRequested state and after the thread responds to abort request, it will go to Stopped state. So what is the function of Aborted state?


回答1:


A thread is blocked if its execution has been suspended by one of the synchronization primitives, such as a lock or a mutex. Any thread that is performing useful work at a given moment is, by definition, not blocked at that moment.

The AbortRequested/Stopped cycle gives the thread an opportunity to perform an orderly shutdown, releasing acquired resources and performing other cleanup tasks.

http://www.albahari.com/threading/




回答2:


Answers to your questions:

  1. I don't believe this mapping would be as useful as you appear to hope. I've never run across one and never needed it.
  2. There isn't a real need for a "Blocked" state unless you're trying to write something like a deadlock detector (rather advanced). From a typical developer perspective, the OS "blocked" state is transient and can be ignored. (It appears that your code is running, but the OS has nothing to do until the async response is received.)
  3. Imagine the Aborted state as .NET providing an exception handler around all the code in the thread. When an exception is caught, causing the thread to die, .NET translates that to an Aborted state for you. Otherwise, you may not be able to tell the difference between abnormal and normal thread termination.


来源:https://stackoverflow.com/questions/8966636/thread-lifecycle-in-net-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!