In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what are the advantages and disadvantages of C# multithreading", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "what are the advantages and disadvantages of C# multithreading?"
1. Basic concepts related to C# multithreading
Process: when a program starts running, it is a process, including the running program and the memory and system resources used by the program. And a process is made up of multiple threads.
Thread: a thread is an execution stream in a program. Each thread has its own proprietary register (stack pointer, program counter, etc.), but the code area is shared, that is, different threads can execute the same function.
Multithreading: multithreading means that a program contains multiple execution streams, that is, multiple different threads can be run simultaneously in a program to perform different tasks, that is, a single program is allowed to create multiple threads of parallel execution to complete their respective tasks.
Static properties: properties that are common to all objects in this class, no matter how many instances of the class you create, but there is only one static property in memory.
Second, the advantages and disadvantages of C# multithreading
Advantages: it can improve the utilization of CPU. In multithreaded programs, when a thread has to wait, CPU can run other threads instead of waiting, which greatly improves the efficiency of the program.
Disadvantages: threads are also programs, so threads need to take up memory, and the more threads, the more memory.
Multithreading needs to be coordinated and managed, so CPU time tracking threads are required.
The access to shared resources between threads will influence each other, so the problem of competing for shared resources must be solved.
Too many threads will lead to too complex control, which may eventually result in a lot of Bug
Classes and methods for controlling threads
Class: using System.Threading; Thread class
The method of the Thread class: Start (): start the thread
Sleep (int): static method that pauses the number of milliseconds specified by the current thread
Abort (): this method is usually used to terminate a thread
Suspend (): this method does not terminate an unfinished thread, it just suspends the thread and can resume later
Resume (): resumes execution of threads suspended by the Suspend () method.
How to manipulate a thread
Using System; using System.Threading; namespace ThreadTest {public class Alpha {public void Beta () {while (true) {Console.WriteLine ("Alpha.Beta is running in its own thread.") } public class Simple {public static int Main () {Console.WriteLine ("Thread Start/Stop/Join Sample"); Alpha oAlpha = new Alpha () / / create a thread to execute the Beta () method of the Alpha class Thread oThread = new Thread (new ThreadStart (oAlpha.Beta)); oThread.Start (); / / the program runs the Alpha.Beta () method while (! oThread.IsAlive) Thread.Sleep (1) / / stop the main thread from 1ms oThread.Abort (); / / terminate the thread oThread oThread.Join (); / / make the main thread wait until the oThread thread ends. You can specify an int parameter as the maximum waiting time Console.WriteLine (); Console.WriteLine ("Alpha.Beta has finished"); try {Console.WriteLine ("Try to restart the Alpha.Beta thread"); oThread.Start () } catch (ThreadStateException) {Console.Write ("ThreadStateException trying to restart Alpha.Beta. ); Console.WriteLine ("Expected since aborted threads cannot be restarted."); Console.ReadLine ();} return 0;}
5. Thread.ThreadState attribute
Aborted: thread stopped
AbortRequested: the thread's Thread.Abort () method has been called, but the thread has not stopped
Background: thread executes in the background, related to the attribute Thread.IsBackground; does not prevent the termination of the program
Running: thread is running normally
Stopped: thread has been stopped
StopRequested: the thread is being asked to stop
Suspended: the thread has been suspended (in this state, you can rerun it by calling the Resume () method)
SuspendRequested: the thread is requesting to be suspended, but there is no time to respond
Unstarted: Thread.Start () is not called to start running the thread
WaitSleepJoin: the thread is blocked because it calls methods such as Wait (), Sleep () or Join ().
VI. Priority of C# multithreading
From high to low, the Highest,AboveNormal,Normal,BelowNormal,Lowest; system defaults to ThreadPriority.Normal
Code that specifies the priority: myThread.Priority=ThreadPriority.Lowest
These are all the contents of this article entitled "what are the advantages and disadvantages of C# multithreading?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.