C # Thread class Thread preliminary

.NET base class library of System.Threading namespace provides a number of classes and interfaces that support multithreading. This namespace has a lot of class. System.Threading.Thread class is to create and control threads, set its priorities and to obtain its status of the most commonly used classes. He has a lot of ways, here we will compare common and important ways to do some introduction:
    Thread.Start execution started threads;: () Thread.Suspend (): suspends the thread, or if the thread has been suspended, does not work; Thread.Resume (): continuing a suspended thread; Thread.Interrupt (): suspended in the Wait or Sleep or Join thread thread state; the Thread.Join (): block the calling thread until a thread terminates at the time of Thread.Sleep (): the current thread to block the specified number of milliseconds; Thread.Abort (): to start the process of terminating this thread. If the thread has been terminated, not by Thread.Start () to start the thread. By calling Thread.Sleep , Thread.Suspend

  

  

  

  

  

  
  Or Thread.Join can pause / blocking threads. Call Sleep () and Suspend () method mean that the thread will not get CPU time. Both suspended thread method is different, at Sleep () so that the thread to immediately stop the execution, but the call to Suspend () before the method, must reach a safe point common language runtime. A thread can not another thread calls Sleep () method, but you can call Suspend () method makes another thread suspended. Already suspended thread calls Thread.Resume () method will make it continue. No matter how many times Suspend () method to block a thread with a single call Resume () method can make the thread to continue. It has been terminated and has not started threads of execution can not be used to hang. Thread.Sleep ( int x ) the thread to block x milliseconds. Only when the thread is another thread by calling Thread.Interrupt () or Thread.Abort () method, to be awakened.If the calling thread is blocked in Thread.Interrupt () method will change the state of the thread, but will throw ThreadInterupptedException exception, you can catch this exception and make a deal, you can ignore this abnormal termination and let the running thread. Within a certain period of waiting, Thread.Interrupt () and Thread.Abort () can wake up a thread immediately.
    We can use Thread.Abort to permanently destroy a thread () method, and will throw ThreadAbortException exception. The end of the thread can catch an exception but it is difficult to control the recovery, the only way is to call Thread.ResetAbort () to cancel the earlier call, and only when this exception is being caused due to abnormal calling thread. For A and B two threads A thread can use right Thread.Abort () method acts on B thread, but B thread can not call Thread.ResetAbort () to cancel Thread.Abort () operation.
    Thread.Abort () method makes the system quietly destroying threads and do not notify the user. Once implemented Thread.Abort() Operation, the thread can not be restarted. Calling this method does not mean that the thread destroyed immediately, so in order to determine if the thread is destroyed, we can call the Thread.Join () to determine its destruction, the Thread.Join () is a blocking call until the thread is indeed terminated before return. But there may be a thread calls Thread.Interrupt () method to abort another thread, and this thread is waiting for the Thread.Join () returns the call.

   Do not use as much as possible Suspend () method to suspend the thread is blocked, because it can easily lead to deadlock. Suppose you suspend a thread, and the thread resources are needed for other threads, the consequences of what will happen. Therefore, we try to thread a different importance to different priorities, with Thread.Priority () method instead of using Thread.Suspend () method.
  Thread class has a lot of attributes, these attributes are important to our multi-threaded programming have to grasp. Thread.IsAlive property: Gets a value that indicates the status of the current thread of execution. If this thread has been started and has not been properly terminated or suspended, compared to true ; otherwise false . Thread.Name property: Gets or sets the name of the thread. Thread.Priority properties: Gets or sets a value that indicates the thread scheduling priority. Thread.ThreadState property: Gets a value that contains the state of the current thread.

  

  

  
  

 

Thread state
   System.Threading.Thread.ThreadState attribute defines the thread's execution state. To create a thread from the thread terminates, it must be one in which the state. When a thread is created, it is in Unstarted state, the Thread class Start () method will allow the thread state to Running state, the thread will remain in this state, unless we call the appropriate method to hang it, blocking, destroy or natural termination. If the thread is suspended, it will be in Suspended state, unless we invoke Resume () method to perform it again, this time to re-thread will become Running state. Once the thread is destroyed or termination of thread is in Stopped state. Thread in this state will cease to exist, as a thread started, the thread will not return to Unstarted state. There is also a thread Background state, which indicates that the thread running in the foreground or background. In a certain time, the thread may be in multiple states. According to an example, one thread is called Sleep and in a blocked, and then another thread calls Abort method on the blocked thread, the thread will also be in this time WaitSleepJoin andAbortRequested state. Once a thread response into Sle blocked or aborted when the destruction will throw ThreadAbortException exception.

Thread priority
   System.Threading.Thread.Priority enumerates priority thread, which determines how many threads can get CPU time. High priority threads will usually get more than the average priority thread CPU time, if more than one high-priority thread, the operating system will cycle between these threads allocated CPU time. Low priority threads obtained CPU time is relatively small, when there is no higher priority threads, the operating system at a selected low priority execution thread. Once the low-priority thread encounters a high priority thread is executed, it will allow the CPU to the high-priority thread. Thread priority for the newly created general priority, we can set the priority level of the thread value, as shown below:

  Highest
  AboveNormal
  Normal
  BelowNormal
  Lowest

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2011/08/11/2135284.html

Guess you like

Origin blog.csdn.net/weixin_33834137/article/details/93494964