"Inside C #" notes (13) multithreading

Source: Internet
Author: User
Tags thread class win32

By dividing one task into multiple tasks, executing on separate threads can make more efficient use of processor resources and save time. But if multi-threading is used unreasonably, it can cause problems and slow down the running speed.

Thread Foundation

A) threading vs. multitasking

A thread is a processing unit, and multiple threads execute concurrently when multitasking. Multitasking involves issues of collaboration and prioritization between tasks. The Windows NT kernel's operating system uses the preemption multitasking mechanism (preemptivemultitasking), which divides the specified execution time (time slice) for each thread, and the thread executes on a given time slice in turn.

When multi-threading is used on a single-core processor, the thread is simply rotated, but it feels as if it is executing at the same time, and if multiple threads are running on multiple cores or multiple processors, the thread will actually run concurrently.

b) Context switch (contextual switching)

For multithreading, context switching is an essential feature. When a time slice is exhausted, the processor sends out an interrupt signal, the processor saves the current thread-related content to the stack, and then reads the contents of the next thread from the stack into the context data structure, and if the time slice runs out, it switches the thread loaded in the context.

c) multi-threaded use

A simple example of multithreaded use is as follows:

?

Threadstartworker = new ThreadStart (Workerthreadmethod); This notation has been approached in the delegate chapter, Xvarname=new X (MethodName), which represents the initialization of a delegate of type X and points to the MethodName method.

When you instantiate a thread, you must pass a delegate of type ThreadStart as a parameter. When the Thread.Start method is called, the method that ThreadStart points to is executed on another thread.

Two System.Threading.Thread class

A) AppDomain ( application domain)

There are many similarities between the AppDomain and the Win32 process, but in Win32, a thread is confined to a particular process, and the threads of different processes cannot interact. Threads in the AppDomain can communicate across domains, or Invoke methods in other AppDomain. Therefore, the AppDomain corresponds to a logical process in the physical process.

b) There are two ways to get a thread instance, one is to use the keyword new, and the other is to use Thread.CurrentThread to get the executing thread. The Thread.Sleep method can be used to suspend a thread for a specified time, and this method is static. If the parameter passed for the Thread.Sleep method is 0, the current thread will voluntarily free the unused time slice, and if it passes timeout.infinite (a constant of 1), then the thread will be suspended indefinitely until there is another one calling the Thread.Interrupt on its instance. Method. The Thread.Suspend method can also suspend a thread, but it is not a static method and is called by another thread, and the method with which it is used is also thread.resume.

c) The Thread.Abort method destroys the thread and terminates the thread by throwing a ThreadAbortException exception inside the system. ThreadAbortException is more special and cannot be captured. After abort, the thread does not stop immediately, and will wait until the current work is completed and the "Safe Point" is reached before exiting. In multithreaded programming, in order to reliably know whether a thread has terminated, you can use the Thread.Join method, which is not returned until the thread is destroyed. In addition, once the thread is destroyed, it cannot be restarted.

Learn Learning materials: Inside C # by Tom Archer

"Inside C #" notes (13) multithreading

Related Article

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.