Several methods for creating and using multithreading and Multithreading

Source: Internet
Author: User

Several methods for creating and using multithreading and Multithreading

I have recently learned a multithreading technology. I want to list it based on my own understanding. I hope I can share with you that multithreading is a method that can save the computing time of the program, this greatly improves the computing efficiency of the program. First, let's talk about the process and thread concepts:

A program contains more than one process, and a process can contain more than one thread. Each process has its own independent memory space, all threads in a corresponding process share the memory space.

Process: A running activity of a program with certain independent functions about a data set. It is the basic unit of dynamic execution in the operating system. In the traditional operating system, a process is both a basic resource allocation unit and a basic execution unit. Independent address spaces make it very difficult for different processes to share state information. To share information, they must be explicitly used (process communication mechanism), and communication between processes is slow, because Process Control and the program communication mechanism are costly.

Thread: processes are the basic units for resource allocation, while threads are the basic units for independent running and independent scheduling. Because threads are smaller than processes, they basically do not have system resources, therefore, the overhead of scheduling is much lower, which can improve the degree of concurrent execution among multiple programs in the system more efficiently. A thread is a logical stream running in the process context. The modern operating system allows us to write a program that runs multiple threads simultaneously in a process. The thread is scheduled by the kernel. Each process starts its life cycle with a single thread. This thread is called the main thread. At a certain time point, the main thread creates a peering thread. From this point in time, the two threads run concurrently. Finally, because the main thread executes a slow System Call, such as read or sleep, the control is passed to the peer thread through context switching. Before controlling the transfer back to the main thread, the peer thread will execute for a period of time.

 

(1) the traditional program execution method is a sequential execution method, that is, after the execution of this sentence is completed, the next sentence will be executed.

When calling a function or executing a digital block, executing the program takes up the execution time of the main program of the system. That is to say, if the function or code block is not executed, the system will not execute the next command:

For example, we call [self doOneThing] in the main function; [self doTheSecondThing]; the next function will be executed only after the first correct execution.

 

(2) multi-threaded program blocks can be executed concurrently, greatly saving program execution time. The following lists several methods for creating multithreading:

(1) Use javasmselectorinbackground to create a thread: that is to say, when the program executes doThing, It is synchronized with the execution of the next program.

[Self defined mselectorinbackground: @ selector (doThing) withObject: nil];

    

(2) directly create a thread. The selector specifies the operation to be performed. When this method is used, the thread starts directly:

[NSThread detachNewThreadSelector: @ selector (doThing) toTarget: self withObject: nil];

      

(3) create a thread and set the Startup Mode

NSThread * thread = [[NSThread alloc] initWithTarget: self selector: @ selector (doThing) object: nil];

Thread. name = @ "QingYun ";

[Thread start];

 

(4) create a multi-thread queue

NSOperationQueue * queue = [[NSOperationQueue alloc] init];

Create an operation object that defines the multi-threaded tasks that need to be executed.

NSInvocationOperation * operation = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (doThing) object: nil];

[Queue addOperation: operation]; // put the operation object in this queue so that the thread can be started directly.

 

(5) Use Block operation objects to create a thread, add it to the queue, and start the thread.

NSOperationQueue * queue = [[NSOperationQueue alloc] init];

NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock: ^ {

NSLog (@ "do thing ....");

[NSThread sleepForTimeInterval: 10];}

[Queue addOperation: operation];

 

(6) use the queue distribution mode to create a thread. The following uses the asynchronous distribution creation thread as an example to describe its working principle. In the asynchronous thread, the system distributes the threads in order during the distribution, but in the execution process, there is a competition between threads.

Dispatch_queue_t queque = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 );

Dispatch_async (queque, ^ {[NSThreadsleepForTimeInterval: 5] ;};

Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_LOW, 0), ^ {[NSThreadsleepForTimeInterval: 5];};

    

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.