C # threads from unfamiliar to familiar (2)

Source: Internet
Author: User
Document directory
  • First, let's look at the thread status
  • Thread status

 

First, let's look at the thread status.

Table 1-1 enumeration constants defined by threadstate

Member name

Description

Aborted

The thread is in the stopped status.

Abortrequested

The thread. Abort method has been called for the thread, but the thread has not received the pending system. Threading. threadabortexception trying to terminate it.

Background

The thread is being executed as the background thread (relative to the foreground thread ). This status can be controlled by setting the thread. isbackground attribute.

Running

The thread has been started, it is not blocked, and there is no pending threadabortexception

Stopped

Thread stopped

Stoprequested

Requesting thread to stop. This is only used internally

Sushortded

Thread suspended

Suspendrequested

Pending request thread

Unstarted

The thread. Start method has not been called for the thread

Waitsleepjoin

The thread has been blocked because wait, sleep, or join is called.

The threadstate attribute of the thread object provides a bit mask defined by threadstate, which indicates the current state of the thread. A thread is at least always in a possible state defined in the threadstate enumeration and can be in multiple States at the same time.

Note: You can only use the thread state in some debugging schemes, instead of using the thread state in the code to synchronize thread activity.

When a managed thread is created, the thread is in the unstarted state. The thread will remain in the unstarted State until it is scheduled by the operating system to the started state. Call the start method to make the operating system know that the thread can be started, but it does not directly change the thread status. Once a thread is in the starting state, many operations can be performed to change the state of the thread. Table 1-2 lists the operations for changing the status and the corresponding new status.

Table 1-2 operations that change the thread status and corresponding new statuses

Operation

Threadstate

Create a thread in the Common Language Runtime Library

Unstarted

Thread call start

Unstarted

The thread starts running.

Running

Continue table

Operation

Threadstate

Thread calls sleep

Waitsleepjoin

The thread calls wait for other objects.

Waitsleepjoin

The thread calls join for other threads.

Waitsleepjoin

Another thread calls interrupt

Running

Another thread calls suspend

Suspendrequested

The thread responds to the suspend request.

Sushortded

Another thread calls resume

Running

Another thread calls abort

Abortrequested

The thread responds to the abort request.

Stopped

Thread terminated

Stopped

Abort method. After this method is called, a threadabortexception exception is thrown in the current thread. This exception causes thread death! The thread enters the stopped status and cannot be restarted!
See the following example.

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading; namespace consoleapplication4 {class program {static void main (string [] ARGs) {thread T1 = new thread (print); // t1.abort (); causes threadabortexception exception t1.start (); // t1.abort (); // t1.join (); blocks the calling thread until the end of a thread. Console. writeline ("called by the main thread"); // t1.start (); threadabortexception exception cannot be restarted} static void print () {thread. sleep (1, 2000); console. writeline ("thread called! ");}}}

In this example, we can see that when the thread is in the unstarted state and the abort () method is called, an exception is thrown!
Open the second comment and get the following result:


Thread terminated! Opening the fourth comment will cause an exception again!
Here I want to explain the join () method. Open the third comment and the following result is displayed,

The function of the join method is to block the thread that calls him. In other words, the thread that calls him is in the blocking state and continues to be executed until the thread execution ends! The main thread is called here!

Interrupt this method interrupts a thread in the waitsleepjoin state.

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading; namespace consoleapplication5 {class program {static void main (string [] ARGs) {thread T1 = new thread (print); t1.start (); thread. sleep (500); t1.interrupt (); thread. sleep (1, 500); console. writeline ("the main line execution ends! ");} Static void print () {try {thread t2 = new thread (print2); t2.start (); t2.join () ;}catch (exception e) {console. writeline ("caught exception: {0}", E. tostring ();} console. writeline ("thread 1 running! ");} Static void print2 () {thread. Sleep (2000); console. writeline (" thread 2 run! ");}}}

The running result is

We can see that thread 1 is interrupted when calling the same method as thread 2, so that thread 1 runs directly! This is the role of interrupt! If you comment out the interrupt method, you will see that thread 2 is running and thread 1 continues to run! Interrupt this method interrupts a thread in the waitsleepjoin state. It should be understandable!

Okay, this is the time!

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.