In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "C# .NET multithreaded application example analysis". In daily operation, I believe that many people have doubts about C# .NET multithreaded application example analysis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "C# .NET multithreaded application example analysis". Next, please follow the editor to study!
a. Start thread
As the name implies, "start a thread" means to create and start a new thread, which can be implemented by the following code:
Thread thread1 = new Thread (new ThreadStart (Count))
Where Count is the function to be executed by the new thread.
b. Kill thread
To "kill a thread" is to root out a thread. In order not to waste effort, you can determine whether a thread is still alive (through the IsAlive property) before killing it, and then you can call the Abort method to kill the thread.
c. Pause thread
It means to let a running thread sleep for a period of time. For example, thread.Sleep (1000); let the thread sleep for 1 second.
d. Priority
There's no need to explain this. There is a ThreadPriority property in the Thread class, which is used to set the priority, but there is no guarantee that the operating system will accept it. A thread can be divided into five priorities: Normal, AboveNormal, BelowNormal, Highest, and Lowest. Specific implementation examples are as follows:
Thread.Priority = ThreadPriority.Highest
e. Suspend thread
The Suspend method of the Thread class is used to suspend the thread until the Resume is called before the thread can continue execution. If the thread is suspended, it will not work.
If (thread.ThreadState = ThreadState.Running) {thread.Suspend ();}
f. Recovery thread
Used to restore a suspended thread so that it can continue to execute, and if the thread is not suspended, it will not work.
If (thread.ThreadState = ThreadState.Suspended) {thread.Resume ();}
An example is listed below to illustrate simple threading capabilities. This example comes from the help documentation.
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; iTunes +) {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; iTunes +) {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 from this code is similar to the following:
Main thread: Start a second thread. Main thread: Do some work. ThreadProc: 0 Main thread: Do some work. ThreadProc: 1 Main thread: Do some work. ThreadProc: 2 Main thread: 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 has returned. Press Enter to end program. At this point, the study on "C# .NET multithreaded application example analysis" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.