Multithreading--Creating a new thread

Source: Internet
Author: User


one, simple single thread

     

A thread is the execution sequence or execution path of the code. The thread-to-process concept is similar in that it resembles a lightweight process; an operating system can run multiple processes, and multiple threads can run within a process. Each application runs at least one thread. When there is only one thread, called a single-threaded application, the thread is created automatically by the system.

Let's look at a simple single-threaded example:





For example, this short program enters from the entry function main and then executes once to the end.


Execution Main The thread of a method is usually the main path, and the entry of the main thread is the starting point of the application. For single-threaded applications, the execution of code is sequential.





However, in multi-threading, when a new thread is manually turned on, the main thread will no longer wait for the new thread to be opened and execute the following code directly.




Two, create a new thread entry method


Here are two ways to enter new threads for two entry methods:



First, introduce the basic operation class for threading:







Here's how to create a thread:



Namespace workerthreadtest{class Program {static void Main (string[] args) {#region Threa DStart defines an entry method without parameters//threadstart ts = new ThreadStart (threadentry);//New Thread entry//thread worker                = new Thread (TS); Worker.  Start ();                Call the Start method of the thread; The////start method is non-blocking, and immediately after being called, the new thread is not executed immediately after////call start, depending on the threading management policy of the operating system.                It's just that the time is very short and it feels like the new thread is executing immediately.                Console.WriteLine ("Main thread Ends");                        Console.readkey (); The method parameter defined by the #endregion #region Parameterizedthreadstart delegate//parameterizedthreadstart is of type object, so the thread's Ingress method t                Hreadentry internal need to do a bit of type conversion parameterizedthreadstart ts = new Parameterizedthreadstart (threadentry);                Thread worker = new Thread (TS);                string[] obj = {"Item3", "Item4"}; Worker.  Start (obj); Open the parameter Thread Console.WriteLine ("Main thread ENDS ");                            Console.readkey (); #endregion} static void Threadentry (Object obj) {Shareresource resource = new Shareresou            Rce ();            String[] ItemArray = (string[]) obj; foreach (string item in ItemArray) {resource.            ADD (item); }}//new thread to open static void Threadentry () {Shareresource resource = new Sha            Reresource (); Resource. Add ("item");//Add Resource}} public class Shareresource {public list<string> List = new List<st        ring> {"Item0", "Item1", "Item2"};            public void Add (string item) {Console.WriteLine ("Add:" + Item); List.        ADD (item); }    }   }


Creating a new thread can be done using both methods: The ThreadStart method and the Parameterizedthreadstart method, which define the parameterless entry method and the parameter entry method respectively.










Multithreading--Creating a new thread

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.