The difference between multi-threading and single thread

Source: Internet
Author: User

The difference between single thread and multithreading

(a) First look at the CPU:

With the main frequency (CPU core operating clock frequency, indicating the speed of the digital pulse signal oscillation in the CPU, equal to FSB (system basic time) multiply frequency), the hardware of the X86 frame becomes the bottleneck, the maximum is 4G, in fact the current 3.6G CPU is near the peak.

The purpose of multithreaded programming is to "maximize the use of CPU resources", when the processing of a thread does not need to occupy the CPU and only the I/O, OEM BIOS and other resources to deal with, so that the need to consume CPU resources of other threads have the opportunity to gain CPU resources.

Each program executes with a process, and each process must have at least one main thread.

This thread is actually a clue to the execution of the process, and you can add additional threads to the process in addition to the main thread, which in some way can be seen as adding multitasking to an application.

when the program is running, you can suspend or run these threads based on various conditions, especially in multi-CPU environments, where these threads are running concurrently. Multithreading is having multiple threads within a process. This allows an application to have multi-tasking capabilities.

Multi-process technology can also achieve this, but the creation process of high consumption (each process has a separate data and code space), communication between processes inconvenient (message mechanism), process switching time is too long, which led to the proposed multithreading, for a single CPU (not open Hyper-threading), Only one thread can be executed at the same time, so if you want to implement multitasking, you can only get one time slice per process or thread, and in one time slice, only one thread executes, and then the other thread executes according to one of the policies. Since the time slice is very short, this gives the user the feeling that there are a lot of threads executing at the same time. However, thread switching is a cost, if the occurrence of a long time, there is a program of suspended animation, the program is unresponsive and other symptoms.

So if you take a multi-process, then you need to switch the memory required for the process that the thread is affiliated to, which costs a lot of time. Thread switching costs little, and threads can share memory. So using multithreading is much less expensive to switch on than many processes.

However, thread switching is still time consuming, so it takes more time to execute a process with two threads than it takes to perform two tasks on a single thread's process.

The use of multithreading does not improve the execution speed of the program, but will reduce the speed, but for the user, can reduce the user's response time.

The above results are only for single CPU, if the multi-CPU or CPU using Hyper-Threading technology, the adoption of multithreading technology will improve the execution speed of the program. Because a single thread is only mapped to one CPU, and multithreading is mapped to multiple CPUs, Hyper-Threading technology is inherently multithreaded, so it also speeds up the execution of the program.

The above document refers to the "Douglax" View: http://blog.csdn.net/douglax/article/details/1532258

The following simple application example is a multithreaded process
private void Button1_Click (Object Sender,eventargs e)
{
Thread T=new Thread (new ThreadStart (ThreadProc));
T.start ();
Thread.Sleep (0);
T.join ();
}
public static void ThreadProc ()
{
String Threa= "Here gets the stream of the UDP pass, which is of type byte[]";
Thread.Sleep (0);
}

1) The window of the program runs on the main thread;
2) thread t=new thread (new ThreadStart (ThreadProc)) and another thread started

The difference between multi-threading and single 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.