6 ways IOS creates and uses multithreading

Source: Internet
Author: User

Non-original text from: http://www.cnblogs.com/yuanjianguo2012/p/3725480.html purely copy

Process and Threading Concepts:

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

Process: is a program with a certain set of independent functions about a data collection of one run activity. It is the basic unit of the operating system dynamic execution, in the traditional operating system, the process is not only the basic resource allocation unit, but also the basic execution unit. Separate address spaces make it very difficult for different processes to share state information, and in order to share information, they must be explicitly used (process communication mechanisms), and communication between processes is slow because the process control and the incoming communication mechanism are expensive.

Thread: The process as the basic unit of allocating resources, and the thread as the basic unit of independent operation and independent dispatch, because the thread is smaller than the process, basically does not have the system resources, so the cost of its scheduling will be much smaller, can more efficiently improve the degree of concurrent execution among multiple programs within the system. A thread is a logical flow that runs in the context of a process. Modern operating systems allow us to write programs that run multiple threads at the same time in a process. Threads are dispatched by the kernel. Each process starts its life cycle as a single thread, which is called the main thread, and at some point the main thread creates a peer. Starting at this point in time, two threads run concurrently. Finally, because the main thread executes a slow system call, such as read or sleep, the control is passed through the context switch to the peer thread. The peer thread executes for a period of time before the control is passed back to the main thread.

(i) Traditional procedures are implemented in a sequential manner, 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 the execution time of the system's main program, that is, the function or block of code does not execute the next instruction after the system executes:

For example: We invoke [self doonething] in the main function;  [Self dothesecondthing]; The next function executes only if the first one is executed correctly.

(b) Multithreaded under the program block can be executed concurrently, greatly saving the execution time of the program, the following list of several ways to create multithreading:

(1) Use Performselectorinbackground to create a thread: that is, the program executes dothing and executes the next segment of the program is executed synchronously

[Self Performselectorinbackground: @selector (dothing) Withobject:nil];

    

(2) Create a thread directly, the selector selector indicates the action to be made, and the thread starts directly with this method:

[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-threaded queue

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

creates an action object that defines the multithreading required to perform the service

Nsinvocationoperation *operation = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (dothing) Object:nil];

[Queue addoperation:operation]; //Put the operand in this queue so that the thread can start directly

(5) Create a thread, add it to the queue, and start the thread by using block blocks to manipulate the object

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

Nsblockoperation *operation = [Nsblockoperation blockoperationwithblock:^{

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

[Nsthread Sleepfortimeinterval:10];

}

[Queue addoperation:operation];

(6) Create a thread using the queue distribution mode: The following is an example of how asynchronous distribution creation threads work, in which the system distributes the threads sequentially in the asynchronous thread at distribution, but in the process of execution, there is a competing relationship between the 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];

};

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.