IOS supports three levels of thread programming, from the underlying layer to the high-level (the higher the level, the more convenient to use, the more concise) are:
1: thread;
2: cocoa operations;
3: Grand Central Dispatch;
Introduction:
ThreadIt has the lowest abstraction level. The other two thread applications encapsulate thread. For programmers, thread is relatively troublesome and requires programmers to manage thread cycles, but the efficiency is the highest. There are two types of threads: Cocoa
Threads -- use nsthread or use the nsobject class method javasmselectorinbackground: withobject: to create a thread; POSIX Threads: a multi-thread Library Based on the C language.
There are three ways to create nsthread:
I. [nsthread detachnewthreadselector: @ selector (mythreadmethod :) totarget: Self withobject: Nil]; call to create a new thread to execute the operation immediately
Ii. nsthread * mythread = [[nsthread alloc] initwithtarget: Self selector: @ selector (mythreadmethod :)
Object: Nil]; [mythread start]; after nsthread initialization, the new thread does not execute, but calls
The thread will be created for execution only when start is enabled. This method is more flexible than the above method. Before starting a new thread, you can perform operations on the thread, such as setting the priority and locking.
3: [myobj implements mselectorinbackground: @ selector (mythreadmainmethod)
Withobject: Nil]; Use the nsobject class method to create a thread:
All of the above can be called in the new thread:
Withobject: waituntildone: update the UI because the sub-thread cannot directly update the UI.
Thread Synchronization and lock:
In many cases, multiple threads access the same data. To avoid conflicts between thread a and thread B and execution sequence, you need to use nscondition and nslock, ensure thread (atomic operation) Security.
The sequence of synchronous execution with nscodition, nscodition
It is a special type of lock, which can be used to synchronize the sequence of Operation execution. The difference between it and mutex is that it is more accurate. Wait until a nscondtion thread is locked until other threads send a signal to that condition. The following is an example:
-(Void) applicationdidfinishlaunching :( uiapplication *) Application {
Tickets = 100;
Count = 0;
Ticketcondition = [[nscondition alloc] init]; // Lock Object
Ticketsthreadone = [[nsthread alloc] initwithtarget: Self selector: @ selector (run) object: Nil];
[Ticketsthreadone setname: @ "thread-1"];
[Ticketsthreadone start];
TicketsThreadtwo = [[NSThread alloc] initWithTarget: self selector: @ selector (run) object: nil];
[TicketsThreadtwo setName: @ "Thread-2"];
[TicketsThreadtwo start];
[Window makeKeyAndVisible];
}
-(Void) run {
While (TRUE ){
[TicketsCondition lock]; //
Lock
If (tickets> 0 ){
[NSThread sleepForTimeInterval: 0.5];
Count = 100-tickets;
NSLog (@ "Current ticket count: % d, sold: % d, thread name: % @", tickets, count, [[NSThread currentThread] name]);
Tickets --;
} Else {
Break;
}
[TicketsCondition unlock];
}
}
Inter-thread communication and interaction
Do things in the main thread of the application:
Performselectoronmainthread: withobject: waituntildone:
Performselecstmmainthread: withobject: waituntildone: modes:
Do things in the specified Thread:
Performselector: onthread: withobject: waituntildone:
Performselector: onthread: withobject: waituntildone: modes:
Do things in the current thread:
Optional mselector: withobject: afterdelay:
Optional mselector: withobject: afterdelay: inmodes:
Cancels a message sent to the current thread.
Cancelpreviousperformrequestswithtarget:
Cancelpreviousperformrequestswithtarget: selector: object:
Cocoa Operetions