. Net Component Programming thread and concurrency Management (1)

Source: Internet
Author: User

. Net Component Programming thread and concurrency Management (1)

1. Thread
  • Thread
  • Thread Creation
  • Thread Blocking
    • Thread Suspension
    • Thread sleep
    • Add thread
    • Thread abort

Currently, almost all applications are multi-threaded. It seems to the user to be an application interface (Application Thread). No matter what operation, it will not lead to slow response on the interface, these are the merits of multithreading. with multithreading, applications can process more operations as much as possible, and multiple threads can be mobilized to process requests in parallel, in this way, the application has a higher system throughput.

1. Thread

Thread 1.1

What is a thread? A thread is an execution path in a process. Each application runs on at least one thread. The thread will be explained in this article.

In. NET, a thread is the basic unit of execution. A thread in. NET is not a physical thread in the operating system, but a hosted code representation of the physical thread.

In the. NET System. Threading namespace, there is a Thread type class, which represents the managed Thread.

1.2 thread Creation

Let's look at the example of thread creation:

1     public class ThreadingMethodCase2     {3         public static void ThreadingMethod()4         {5             Thread thread = Thread.CurrentThread;6             int threadid = thread.ManagedThreadId;7             Console.WriteLine(thread.Name + "ThreadID is " + threadid);8         }9     }
1 ThreadStart threadstart = new ThreadStart(ThreadingMethodCase.ThreadingMethod);2 Thread thread = new Thread(threadstart);3 thread.Name = "SubThread";4 thread.Start();

Creating a thread in. NET requires the thread method first. What is the thread method? First, the thread represents an operation or a group of operations. The thread method is the [operation] in the previous sentence ].
Here, let's take a look at the fact that the constructor of the Thread type accepts a non-parameter Delegate of the ThreadStart type, which is understandable.
The Start () function of a thread that must be displayed before it can be executed. Calling the Strat () function does not block the current thread, that is, after the call, the control immediately returns to the client of the current thread.

1.3 thread blocking 1.3.1 thread Suspension
1 public sealed class Thread2 {3    public void Suspend();4    public void Resume();5    ……6 }

The Suspend () method suspends the thread execution.

The Resume () method is to release the suspended thread so that the thread can continue to execute it. When the Suspend () method is called, the current thread will not be blocked. After the method is called, the control will return immediately, in addition, in the thread to be suspended, it is not immediately suspended, but will only execute the hanging operation when it is executed to a security point. What is security? For example, if a function is being executed in the thread to be suspended and the external notification command is to be suspended, the thread will not be suspended at this time, when this function is executed, the thread is suspended. Assume that the Editor inserts the security point at the end of each function.

In. NET, we do not recommend using these two functions, because it may cause instability in many aspects.

1.3.2 thread sleep

 

1 public sealed class Thread2 {3    public static void Sleep(int millisecondsTimeout);4    public static void Sleep(TimeSpan timeout);5    ……6 }

Sleep () is a static function that blocks the call and causes the current thread to discard the CPU time slice. It means that the control will be returned to the called thread after the specified time of Sleep.

Thread. Sleep (20); // Sleep the current call Thread for 20 milliseconds

The Thread class also provides an operation similar to sleep:

1 public static void SpinWait(int iterations);

Calling SpinWait () will also cause the current congestion, but the current thread will not discard the CPU time slice, but will continue to execute after a limited time (parameter setting, this kind of function is called in this way under Controllable conditions. For example, if the current thread needs to use a resource which is used by other threads, you can use the SpinWait () function, after waiting for a limited amount of time, read the resource again. If the resource has not been released by other threads, the current thread will continue to execute. This is a method that can be controlled.

1.3.3 Add thread

The Join () method of the Thread class allows one Thread to wait for the end of another Thread.

What does this definition mean? Let's take a look at the sample code of Thread. Join:

 1     public class StudyCase 2     { 3         public void ThreadingTest() 4         { 5             ThreadStart threadstart = new ThreadStart(ThreadingMethodCase.ThreadingMethod); 6             Thread thread = new Thread(threadstart); 7             thread.Name = "newThread"; 8             Thread.CurrentThread.Name = "CurrentThread"; 9             for (int i = 0; i < 5; i++)10             {11                 if (i == 0)12                 {13                     thread.Start();14                     thread.Join();15                 }16                 Console.WriteLine(Thread.CurrentThread.Name+"_"+i.ToString());17             }18         }19 20     }

 

1     public class ThreadingMethodCase2     {3         public static void ThreadingMethod()4         {5             Thread thread = Thread.CurrentThread;6             int threadid = thread.ManagedThreadId;7             Console.WriteLine(thread.Name + "ThreadID is " + threadid);8         }9     }

From the result, let's take a look at the code. We can see the meaning of Join, which is to block the current thread and wait until the sub-thread (the thread variable in the ThreadingTest function) is executed, the current thread is executed again.

1.3.4 thread abort

The Abort () method provided by the Thread type can be called to stop the running of the Thread, and the aborted Thread will throw a ThreadAbortException type exception. The following sample code is clearly described and the sample code result diagram.
Let's look at the sample code:

1 public class ThreadingMethodCase 2 {3 public static void ThreadingMethodDiv () 4 {5 try 6 {7 while (true) 8 {9 Console. writeLine ("test subthread abort"); 10} 11} 12 catch (ThreadAbortException abex) 13 {14 Console. writeLine (Thread. currentThread. name + "-subthread internal-" + abex. message); 15} 16} 17} 18 public class StudyCase19 {20 public void ThreadingTestAbort () 21 {22 Thread. currentThread. name = "CurrentThread"; 23 ThreadStart threadstart = new ThreadStart (ThreadingMethodCase. threadingMethod); 24 Thread thread = new Thread (threadstart); 25 thread. name = "newThread"; 26 27 thread. start (); 28 for (int I = 0; I <10; I ++) 29 {30 Console. writeLine (I. toString (); 31} 32 thread. abort (); 33 thread. join (); 34 35 Console. writeLine ("current thread"); 36} 37 38}

Thread Synchronization will be explained in the next chapter.

 

 

 

Author: Jin Yuan

Source: http://www.cnblogs.com/jin-yuan/

The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.

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.