.NET multi-threaded programming (2): system.threading.thread class

zhaozj2021-02-16  44

.NET multi-threaded programming (2): system.threading.thread class

In the next article, I will introduce you to the thread API in .NET, how to create threads, start and stop threads, set priority, and status using C #.

The program written in .NET will be automatically assigned a thread. Let's take a look at the knowledge of creating a thread with a C # programming language and continue learning threads. We all know that the main thread of the .NET runtime environment is started by the main () method, and the .NET compile language has an automatic garbage collection function. This garbage collection occurs in another thread, all of which are In the background, let us not feel what happened. The default here is that there is only one thread to complete all the program tasks, but as we have discussed in the first article, it is possible that we will add more as needed. Multi-thread makes the program better coordination. For example, in our example, a program that needs to draw graphics or complete a large number of computational procedures while entering a user, we must add a thread that allows the user to get a timely response, because the input to time and response is Just, and another thread is responsible for graphical drawing or a large number of calculations.

The system.threading namespace of the .NET underlying class library provides a large number of classes and interfaces to support multi-threaded. This namespace has a lot of classes, we will focus on this class here.

The System.Threading.Thread class is created and controlling the thread, setting the priority of its priority and gets the most commonly used state. He has a lot of ways, here we will introduce more common and important methods:

Thread.Start (): Start the execution of the thread;

Thread.suspend (): Suspend the thread, or if the thread has been suspended;

Thread.Resume (): Continue to hang the thread;

Thread.Interrupt (): Abort the thread in WAIT or SLEEP or JOIN threads;

Thread.join (): Block calling thread until a thread is terminated

Thread.sleep (): Block the current thread blocks the number of milliseconds;

Thread.abort (): The process of starting to terminate this thread. If the thread is already terminated, the thread cannot be started via thread.start ().

By calling thread.sleep, thread.suspend or thread.join can be suspended / blocked. Calling the Sleep () and Suspend () methods means that the thread will no longer get the CPU time. The method of these two pause threads is different, sop () allows the thread to stop execution, but before the Suspend () method is called, a security point must be reached when the public language is running. A thread cannot call another thread to call the SLEEP () method, but you can call the Suspend () method so that another thread is suspended. Call the thread.Resume () method for the thread that has hangs will continue. Regardless of how many Suspend () methods to block a thread, simply calling the resume () method once can continue to be executed. The thread that has been terminated and the thread that has not been executed has not been executed. Thread.sleep (int X) causes the thread to block X milliseconds. Only when the thread is to be used by other threads by calling the thread.Abort () or the thread.abort () method can be woken up. If the thread.Interrupt () method is called for a thread.Interrupt () method for the blocking state, thread state is threaded, but you can capture this exception and do it, or ignore this exception and stop the thread at runtime. Within a certain wait time, Thread.Interrupt () and thread.abort () can immediately wake up a thread. Below we will explain how to stop another thread from a thread. In this case, we can permanently destroy a thread by using the thread.abort () method, and will throw the ThreadAbortException exception. Make the ended thread can capture an exception but it is difficult to control recovery, only the way is to call thread.resetabort () to cancel the call just now, and only when this exception is caused by the modified thread. Therefore, the A thread can correctly use the thread.abort () method to act on the B thread, but the B thread cannot call Thread.ResetAbort () to cancel the thread.abort () operation. The thread.abort () method makes the system quietly destroy the thread and does not notify the user. Once the thread.abort () operation is implemented, the thread cannot be restarted. This method does not mean that the thread is immediately destroyed, so we can call Thread.join () to determine its destruction, thread.join () is a blocking call until the thread is indeed endless. return. However, it is possible to call the thread.interrupt () method to abort another thread, and this thread is waiting for the returning of thread.join () call.

Do not use the suspend () method to hang the blocking thread because it is easy to cause deadlocks. Suppose you hang a thread, and the resources of this thread are what other threads need, what will happen. Therefore, we will use thread.Suspend () methods with a different priority to different priorities as much as possible.

Thread class has a lot of properties, these important properties are our multi-threaded programming must be mastered.

Thread.isalive Attribute: Get a value indicating the execution status of the current thread. True if this thread is started and has not been terminated or aborted; otherwise false. Thread.name Properties: Gets or sets the name of the thread.

Thread.Priority property: Get or set a value that indicates the scheduling priority of the thread.

Thread.ThreadState Properties: Get a value that contains the status of the current thread.

In the following example, we will look at how to set these properties, in the subsequent example we will discuss these properties in detail.

Create a thread, first instantiate a Thread class, call the Threadstart delegation in the class constructor. This delegate contains the thread where to start execution. When the thread is started, the start () method launches a new thread. The following is an example program.

Using system;

Using system.threading;

Namespace Learnereads

{

Class Thread_App

{

Public static void first_thread ()

{

Console.writeline ("First Thread Created");

Thread current_thread = thread.currentthread;

String thread_details = "thread name:" current_thread.name

"/ r / nthread state:" current_thread.threadstate.tostring ()

"/ r / n thread priority level:" current_thread.priority.toString ();

Console.writeline ("The Details of The Thread Are:" Thread_Details;

Console.writeline ("First Thread Terminated");

}

Public static void

Main

()

{

ThreadStart THR_START_FUNC = New ThreadStart (first_thread);

Console.Writeline ("Creating The First Thread");

Thread fthread = new thread (thr_start_func);

fthread.name = "first_thread";

fthread.start (); // Starting the Thread

}

}

}

In this example, an FTHREAD thread object is created, which is responsible for performing the task inside the first_thread () method. The agent of the address threadstart that contains first_thread () when Thread's start () method is called.

Thread status

The system.threading.thread.threadState property defines the status of the thread when executing. Threads are terminated from the creation to the thread, which must be in one of them. When the thread is created, it is in the UnStarted state, the Start () method of the Thread class will make the thread state into the Running state, and the thread will always be in this state unless we call the corresponding method to hang, block, Destruction or nature termination. If the thread is suspended, it will be in the SUSPENDED state unless we call the resume () method to re-execute it, this time the thread will revert to Running status. Once the thread is destroyed or terminated, the thread is at the STOPPED state. Threads in this state will not be present, as thread starts start, thread will not be able to return to UNSTARTED state. The thread has a Background status, which indicates that the thread runs at the front desk or a background. In a determined time, the thread may be in multiple states. According to examples, a thread is called Sleep and then another thread calls an Abort method on this blocking thread, which will at the WaitSleepJoin and the AbortRequested status. Once the thread response is swelled or aborted, ThreadAbortException exception is thrown when destroy. Thread priority

System.Threading.Thread.Priority enumerates the priority of the thread, which determines how much CPU time can be obtained. High priority threads typically get more CPU time than threads of the general priority, and if more than one high priority thread is more than one high priority thread, the operating system will allocate the CPU time between these threads. The low priority thread is relatively small, and there is no high priority thread here, the operating system will select the next low priority thread execution. Once the low priority thread encounters a high priority thread, it will give the CPU to high priority threads. The newly created thread priority is a general priority, we can set the priority value of the thread, as shown below:

Highest

Abovenormal

Normal

Belownormal

Lowest

Conclusion: In this section, we discussed the priority of the creation of threads. System.Threading Namespace also includes advanced features of thread lock, thread synchronization, multi-thread management class, and deadlock solution, in the back part we will continue to discuss these content.

转载请注明原文地址:https://www.9cbs.com/read-23285.html

New Post(0)