C # thread state knowledge.

Source: Internet
Author: User

. NETof the base Class librarySystem.Threadingnamespaces provide a number of classes and interfaces that support multithreading. There are many classes in this namespace.System.Threading.ThreadA class is the most common class that creates and controls a thread, sets its priority, and obtains its state. He has a lot of methods, here we will be more common and important methods to do the introduction:
Thread.Start(): Start the execution of the thread;

   Thread.Suspend(): Suspends the thread, or does not work if the thread is suspended;

   Thread.Resume(): Continue the suspended thread;

   Thread.Interrupt(): Abort inWaitOrSleepOrJoinThread State of the thread;

   Thread.Join(): Blocks the calling thread until a thread terminates

   Thread.Sleep(): Blocks the current thread for the specified number of milliseconds;

   Thread.Abort(): To begin the process of terminating this thread. If the thread is already terminating, it cannot be passedThread.Start() to start the thread.
by callingThread.Sleep,Thread.SuspendOrThread.JoinCan pause/Blocks the thread. CallSleep ()AndSuspend ()method means that the thread will no longer getCPUTime. There is a difference between the two methods of suspending a thread,Sleep ()Causes the thread to stop executing immediately, but calls theSuspend ()method, the common language runtime must reach a security point. One thread cannot be called on another threadSleep ()method, but you can call theSuspend ()method causes another thread to pause execution. Called on a thread that has been suspendedThread.Resume() method will keep it executing. No matter how many times you useSuspend ()method to block a thread, calling only one timeResume ()method allows the thread to continue executing. A thread that has been terminated and has not yet started execution cannot use a hang.Thread.Sleep(int x) causes the thread to blockxMilliseconds. Only if the thread is being called by another threadThread.Interrupt() orThread.Abort() method to be awakened. If you call a thread that is in a blocked stateThread.Interrupt() method will change the state of the thread, but will throwthreadinterupptedexceptionException, you can catch the exception and make a deal, or you can ignore the exception and let the runtime terminate the thread. Within a certain waiting time,Thread.Interrupt() andThread.Abort() can wake up a thread immediately.
We can do this by usingThread.Abort() method to permanently destroy a thread, and will throwThreadAbortExceptionAbnormal. The only way to make the terminating thread catch the exception but it is difficult to control recovery is to callThread.resetabort() to cancel just the call, and only if the exception was caused by the calling thread. For both A and b two threads,AThe thread can be used correctlyThread.Abort() Method forBThread, butb thread cannot invoke thread.abort () operation.
    thread.abort () method causes the system to silently destroy the thread without notifying the user. Once implemented thread.abort () operation, the thread cannot be restarted. Calling this method does not mean that the thread is destroyed immediately, so in order to determine whether the thread is destroyed, we can call () to determine its destruction, thread.join () is a blocking call until the thread does terminate before it returns. But it is possible that a thread calls thread.interrupt () method to abort another thread, And this thread is waiting for thread.join () The return of the call.

Don't use it as much as possible.Suspend ()method to suspend a blocking thread because it can easily cause a deadlock. Suppose you suspend a thread, and the resources of that thread are what the other threads need and what happens. As a result, we try to give different priorities to different threads of importance, usingthread.priority() method instead of using theThread.Suspend() method.
The thread class has a lot of attributes, and these are important attributes that we have to master for multithreaded programming.

  thread.isalive Property: Gets a value that indicates the execution state of the current thread. True if this thread has started and has not been terminated or aborted properly ; otherwise, false.

  Thread.Name Property: Gets or sets the name of the thread.

  thread.priority Property: Gets or sets a value that indicates the scheduling priority of the thread.
   thread.threadstate property: Gets a value that contains the state of the current thread.

Thread Status
The System.Threading.Thread.ThreadState property defines the state of the thread at execution time. When a thread terminates from creation to thread, it must be in one of the states. When a thread is created, it is in the unstarted state, and the start () method of the thread class causes the thread state to become running state, and the thread will remain in this state unless we call the appropriate method to suspend, block, destroy, or terminate it naturally. If the thread is suspended, it will be in the suspended state, unless we call the resume () method to re-execute it, and the thread will become running again. Once the thread is destroyed or terminated, the thread is in the stopped state. A thread that is in this state will no longer exist, as the thread will not be able to return to the unstarted state until it starts. The thread also has a background state that indicates whether the thread is running in the foreground or in the background. At a certain time, the thread may be in more than one state. As an example, a thread is called sleep and is blocked, and then another thread calls the Abort method on the blocked thread, when the thread is in both the WaitSleepJoin and abortrequested states. Once the thread response is switched to SLE blocking or aborting, a ThreadAbortException exception is thrown when destroyed.

Thread Priority
System.Threading.Thread.Priority enumerates the priority levels of a thread, which determines how much CPU time the thread can get. High-priority threads typically get more CPU time than normal-priority threads, and if more than one high-priority thread, the operating system will cycle the CPU time between those threads. Low-priority threads get relatively little CPU time, and when there are no high-priority threads, the operating system picks the next low-priority thread to execute. Once a low-priority thread encounters a high-priority thread at execution time, it yields the CPU to the high-priority thread. The newly created thread priority is the general priority, and we can set the value of the thread's priority level, as shown here:

Highest
AboveNormal
Normal
BelowNormal

C # Thread-state knowledge.

Related Article

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.