There are three main methods for iOS Multithreading ( 1) nsthread (2) nsoperation (3) GCD
The following describes the three methods.
1. nsthread
The call method is as follows:
If the function needs to input parameters, it can be passed in from the object. (1) [nsthread detachnewthreadselector: @ selector (threadinmainmethod :) totarget: Self withobject: Nil]; (2) nsthread * mythread = [[nsthread alloc] initwithtarget: Self selector: @ selector (threadinmainmethod :) object: Nil]; [mythread start]; (3) [OBJ returns mselectorinbackground: @ selector (threadme) withobject: Nil];
Question: If a viewcontroller runs a thread and the viewcontroller is release before the thread ends, what will happen?
After testing, the thread does not end, the viewcontroller keeps it, And the dealloc method is not executed.
2. nsoperation
Nsoperation is also a type of multithreading. nsopertaion has two forms: (1) Concurrent execution. You need to reload the following four methods: // execute the main function of the task, thread-running entry function-(void) Start // whether to allow concurrency. Yes is returned, and no is returned. By default, no-(bool) isconcurrent-(bool) isexecuting // indicates whether it has been completed. This must be reloaded. Otherwise, nsopertaion placed in nsoperationqueue cannot be released normally. -(Bool) isfinished, such as testnsoperation: nsoperaion, reload the preceding four methods and declare an nsoperationqueue. Required * queue = [[[nsoperationqueue alloc] init] autorelease: testnsoperation];
It will automatically call the start function in testnsoperation. If you need multiple nsoperation, you need to set some attributes of the queue. If multiple nsoperation dependencies exist, you can also set them, for more information, see the API documentation.
(2) Non-concurrent execution-(void) the main method only needs to be reloaded.
3. GCD
GCD is very powerful and has little experience. But scorpiozj summary is more comprehensive (http://www.cnblogs.com/scorpiozj/archive/2011/07/25/2116459.html)
At the same time, this articleArticleMore detailed http://www.cnblogs.com/vinceoniphone/archive/2011/04/07/2007968.html also introduced
Official tutorial
GCD is closely linked to the block, so it is best to first understand the block (you can view it here ). GCD is a C-level function, which means it also provides the C function pointer as a parameter to facilitate C programmers.
The following describes how to use GCD:
Dispatch_async (dispatch_queue_t queue, dispatch_block_t block );
Async indicates asynchronous running, block indicates what you want to do, and queue indicates who you want to handle the task. (In addition to async, there are also sync and delay. This article takes async as an example ).
Multithreading is used in the program because the program often needs to read data and then update the UI. for a good user experience, Data Reading operations tend to run in the background, so as to avoid blocking the main thread. there are three types of queue in GCD for processing.
1. Main queue:
As the name suggests, running in the main thread is obtained by dispatch_get_main_queue. Main queue is required for UI-related operations.
2. Serial quque (private dispatch Queue)
Each time a task is run, multiple tasks can be added and the execution sequence is FIFO (queue, first-in-first-out). Generally, tasks are generated by programmers, for example:
Dispatch_queue_t mycustomqueue = dispatch_queue_create ("example. mycustomqueue", null );
Dispatch_async (mycustomqueue, ^ {
For (INT abc = 0; ABC <100; ABC ++)
{
Printf ("1.do some work here. \ n ");
}
});
Dispatch_async (mycustomqueue, ^ {
For (INT abc = 0; ABC <100; ABC ++)
{
Printf ("2.do some work here. \ n ");
}
});
Dispatch_queue_t mycustomqueue2 = dispatch_queue_create ("example. mycustomqueue2", null );
Dispatch_async (mycustomqueue2, ^ {
For (INT abc = 0; ABC <100; ABC ++)
{
Printf ("1. mycustomqueue2 do some work here. \ n ");
}
});
Dispatch_async (mycustomqueue2, ^ {
For (INT abc = 0; ABC <100; ABC ++)
{
Printf ("2. mycustomqueue2 do some work here. \ n ");
}
});
The printed result must be: however, becauseMycustomqueue and Mycustomqueue2 Is in two queues, so in the queue In mycustomqueue:
"1. Do some work here.In the2. Do some work here.,InMycustomqueue2 Queue:"1". Mycustomqueue2
Do some work here.In the2. mycustomqueue2 do some work here.Before. InMycustomqueue andMycustomqueue2
There are no tasks in sequence. And is not concurrent.
3. Concurrent Queue (Global dispatch Queue ):
You can run multiple tasks at the same time. The start time of each task is in the order of adding queue, and the end order depends on the respective tasks. Use dispatch_get_global_queue to obtain the task.
So we can get a general idea about the GCD framework:
Dispatch_queue_t newthread = dispatch_get_global_queue (dispatch_queue_priority_default, 0 );
Dispatch_async (newthread, ^ {[Selfdownloadimage: imageurl];
});
Section:Nsthread mode may be able to perform faster switching, because armv6 or later processors provide a very powerful thread switching mechanism. But nsthread does not
Multi-core distribution, This system interface must first ensure the reliability of user threads. Grand Central Dispatch explicitly uses the dispatch queue for multi-core
Dispatch scheduling, so g_c_d is faster if it is on a multi-core processor. If your processor is a single core, you can use a faster nsthread switch.
Where do I find GCD?
1. GCD is part of libsystem. dylib
2. # include <dispatch/dispatch. h>