C # written multithreading

xiaoxiao2021-04-06  287

I heard this thing early in C #, but I have never used it before, and now I have tried it, it hits electricity.

Write multi-threads in C # .NET defines the multi-thread function in the System.Threading namespace. Therefore, to use multithreading, you must first declare this name space (use system.threading;).

Even if you don't have experience in multi-threaded applications, you may have heard of "starting thread" "killing threads", in fact, in addition to these two, there are many threads, such as "suspend thread" priority "Hanging Thread" "Restore Thread", etc. Next, one will be explained below.

a. Start thread

As the name, "Start Thread" is new and started with a thread, and the following code can be implemented:

Thread thread1 = new thread (new threadstart (count);

The count is a function that will be executed by the new thread.

b. Kill threads

"Kill thread" is to smash the first thread, in order not to make a good gas, it is best to judge whether it is still alive before killing a thread, and then call the Abort method to kill this thread.

C. Term thread

It means to let a running thread sleep for a while. Such as thread.sleep (1000); is to let the thread sleep for 1 second.

d. priority

This is not explained. There is a ThreadPriority property in the thread class that sets priority, but cannot guarantee that the operating system accepts this priority. A thread has a priority of 5: Normal, Abovenormal, Belownormal, Highest, Lowest. Specific implementations are as follows:

Thread.priority = threadpriority.Highest;

e. hang thread

The SUSPEND method of the Thread class is used to suspend the thread, know that the resume can continue to be executed. If the thread has hang, it will not work.

IF (thread.threadstate = threadstate.running)

{

Thread.suspend ();

}

f. recovery thread

It is used to restore the already pending thread to let it continue, and if the thread does not hang, it will not work.

IF (thread.threadstate = threadstate.suspend)

{

Thread.Resume ();

}

An example will be listed below to illustrate a simple thread tension. This example comes from the help document.

using System; using System.Threading; // Simple threading scenario:.. Start a static method running // on a second thread public class ThreadExample {// The ThreadProc method is called when the thread starts // It loops ten times, writing To the console and yielding // the rest of its Time slice each time, and then ends. public static void threadproc () {for (int i = 0; i <10; i ) {console.writeline ("threadproc: {0 } ", i); // yield The rest of the time slice. thread.sleep (0);}} public static void main () {Console.Writeline (" Main Thread: Start a Second Thread. "); // The constructor for the Thread class requires a ThreadStart // delegate that represents the method to be executed on the // thread C # simplifies the creation of this delegate Thread t = new Thread (new ThreadStart (ThreadProc))..; . // Start ThreadProc On a uniprocessor, the thread does not get // any processor time until the main thread yields Uncomment // the Thread.Sleep that follows t.Start () to see the difference t.Start ()..; //Thread.sleep(0); for (int i = 0; I <4; "I ) {console.writeline (" Main Thread: do some work. "); Thread.sleep (0);} console.writeline "Main Thread: Call Join (), To Wait Until ThreadProc Ends."); T.JOIN (); Console.Writeline ("Main Thread: threadproc.join Has Returned. Press Enter to End Program);

Console.readline ();}} The output generated by this code is similar to the following: Main thread: Start a second.

Main trread: do some work.

ThreadProc: 0

Main trread: do some work.

ThreadProc: 1

Main trread: do some work.

ThreadProc: 2

Main trread: do some work.

ThreadProc: 3

Main thread: Call Join (), To Wait Until ThreadProc Ends.

ThreadProc: 4

Threadproc: 5

ThreadProc: 6

ThreadProc: 7

ThreadProc: 8

ThreadProc: 9

Main thread: threadproc.join haas returned. Press Enter to End Program.

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

New Post(0)