Understanding C # Thread Class thread

Source: Internet
Author: User
Tags join sleep terminates thread class

The System.Threading namespace of the. NET Base Class Library provides a large number of classes and interfaces to support multithreading. There are many classes in this namespace. The System.Threading.Thread class is the most common class that creates and controls threads, sets their precedence, and obtains their state. He has a lot of ways, here we will introduce the 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 (): Aborts a thread that is in a wait or sleep or join thread state;

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

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

Thread.Abort (): Begins the process of terminating this thread. If the thread is already terminating, the thread cannot be started by Thread.Start ().

You can suspend/block threads by calling Thread.sleep,thread.suspend or Thread.Join. Calling the sleep () and suspend () methods means that the thread will no longer get CPU time. The two methods of pausing a thread are different, and sleep () causes the thread to stop execution immediately, but before the suspend () method is invoked, the common language runtime must reach a safe point. One thread cannot call the sleep () method on another thread, but you can call the Suspend () method so that another thread suspends execution. Calling the Thread.Resume () method on a thread that has already been suspended causes it to continue executing. No matter how many times the Suspend () method is used to block a thread, the resume () method can be invoked at a time to allow the thread to continue executing. A thread that has been terminated and has not yet started execution cannot use a suspend. Thread.Sleep (int x) blocks the thread for x milliseconds. The thread can be awakened only if it is invoked by another thread by calling the Thread.Interrupt () or the Thread.Abort () method. Calling the Thread.Interrupt () method on a blocked thread will cause the thread state to change, but throws a Threadinterupptedexception exception, which you can catch and handle, or ignore the exception and let the runtime terminate the thread. Within a certain waiting time, both Thread.Interrupt () and Thread.Abort () can immediately wake up a thread.

We can permanently destroy a thread by using the Thread.Abort () method, and will throw a ThreadAbortException exception. So that the terminated thread catches the exception but it is difficult to control recovery, the only way is to call Thread.resetabort () to cancel the call, and only if the exception is caused by the calling thread. For A and b two threads, a thread can correctly use the Thread.Abort () method for a B thread, but a B thread cannot invoke Thread.resetabort () to cancel the Thread.Abort () operation.

The Thread.Abort () method allows the system to silently destroy threads without notifying the user. Once the Thread.Abort () operation is implemented, 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 Thread.Join () to determine its destruction, and Thread.Join () is a blocking call until the thread is really terminated before returning. However, it is possible for one thread to call the Thread.Interrupt () method to abort another thread that is waiting for the return of the Thread.Join () call.

Do not use the Suspend () method as much as possible to suspend the blocking thread because it can easily cause deadlocks. Suppose you suspend a thread, and the resource of this thread is what other threads need, and what happens. Therefore, we use the Thread.priority () method instead of the Thread.Suspend () method as much as possible to give different priorities to threads of different importance.

The thread class has a number of attributes, and these are important attributes that we have to master in multithreaded programming.

Thread.isalive property: Gets a value that indicates the execution state of the current thread. True if this thread is started and has not been properly terminated or aborted, 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. A thread terminates from creation to thread, and it must be in one of these states. When a thread is created, it is in a unstarted state, and the start () method of the thread class changes the thread state to a running state, and the thread remains in this state unless we invoke the appropriate method to suspend, block, destroy, or terminate naturally. If the thread is suspended, it will be in a suspended state, and the thread will be changed back to running state unless we call the resume () method to execute it again. Once the thread is destroyed or terminated, the thread is in a stopped state. A thread in this state will no longer exist, just as the thread starts, the thread will not be able to return to the unstarted state. The thread also has a background state that indicates whether the thread is running in the foreground or backstage. At a certain time, the thread may be in multiple states. As an example, a thread is blocked by invoking sleep, and then another thread calls the Abort method on the blocked thread, at which point the thread will be in both the WaitSleepJoin and abortrequested states. Once the thread response turns to SLE blocking or aborting, a ThreadAbortException exception is thrown when it is 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 usually get more CPU time than the average priority thread, and if more than one high-priority thread is used, the operating system loops over the CPU time between those threads. Low-priority threads get relatively little CPU time, and when there is no high-priority thread, the operating system picks up 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 a high-priority thread. The newly created thread priority is the general priority, and we can set the value of the thread's priority, as shown in the following:

Highest

AboveNormal

Normal

BelowNormal

Lowest

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.