NsthreadI. Creating and starting a thread1. Several ways of the thread1) Create thread first, then start
Object : nil]; [Thread start];
2) automatic direct start after thread creation
[Nsthread detachnewthreadselector: @selector (Run) totarget:self Withobject:nil]; [Self Performselectorinbackground: @selector (Run) Withobject:nil];
3) implicitly create and start threads
[Self Performselectorinbackground: @selector (Run) Withobject:nil];
The advantages and disadvantages of the latter two methods are compared to the first:
- Advantages: Simple and fast
- Cons: Unable to set the thread in more detail
2. Other uses1) Main thread related usage
main thread related usage // get the main thread // Is the main thread // Is the main thread
2) Main thread related usage
Nsthread *current = [Nsthread CurrentThread]; // get current thread
3) Priority of thread invocation
+ (double) threadpriority; + (BOOL) setthreadpriority: (double) p; -(double) threadpriority; -(BOOL) SetThreadPriority: (double) p; /* the scheduling priority value range is 0.0 ~ 1.0, the default 0.5, the higher the value, the higher the priority */
4) The name of the thread
-(void) SetName: (NSString *) n; -(NSString *) name;
Two. Status of the thread
Object : nil]; [Thread start];
1. Start the thread
2. Blocking (pausing) threads
+ (void) Sleepuntildate: (NSDate *) date; + (void) Sleepfortimeinterval: (Nstimeinterval) ti; // Enter blocking State
3. Force Stop Thread
+ (void) exit; // into the state of death
- Note: Once a thread stops (dies), it cannot be opened again
three. Multi-threaded security risks1. Multithreading Security Risks
- A resource may be shared by multiple threads, that is, multiple threads may access the same piece of resources ;
- For example, multiple threads access the same object, the same variable, the same file
- data confusion and data security issues can easily arise when multiple threads access the same resource
2. Examples of multi-threaded security pitfalls1) Withdraw money
2) Sell tickets
3. Security Risk Analysis
4. Security Hidden Trouble solution--Mutual exclusion lock 1) Mutual exclusion lock schematic diagram
2) The use of Mutex lock format
// code that needs to be locked }
- Note: Locking 1 parts of code with only 1 locks, with multiple locks is not valid
3) Advantages and disadvantages of mutual exclusion lock
- Advantages: It can effectively prevent the data security problem caused by multi-thread snatch resource
- Cons: Consumes a lot of CPU resources
4) The use of mutual exclusion lock
- Multiple threads rob the same piece of resources
5) Related professional terminology: thread synchronization
- Thread synchronization means that multiple threads perform tasks sequentially
- Mutex is the use of thread synchronization technology
Four. Atomic and non-atomic properties1. OC has nonatomic and atomic two choices when defining attributes
- Atomic properties, locking for setter methods (default is Atomic)
- Non-atomic attribute, does not lock the setter method
3. Atomic Plus Lock principle
int Age ; -(void) Setage: (int) age{ @synchronized (self) { = age ; }}
4. Comparison of Nnonatomic and atomic
- Atomic: Thread-safe, requires a lot of resources
- Nonatomic: Non-thread-safe, suitable for small memory mobile devices
5. Recommendations for iOS development
- All properties are declared as Nonatomic
- Try to avoid multiple threads robbing the same piece of resources
- As far as possible to locking, resource-grabbing business logic to the server-side processing, reduce the pressure of mobile clients
five. Inter-thread communication1. What is called inter-thread communication
- In 1 processes, threads often do not exist in isolation and require frequent communication between multiple threads
2. The embodiment of inter-thread communication
- 1 Threads pass data to another 1 threads
- After performing a specific task in 1 threads, go to another 1 thread to continue the task
3. Common methods for inter-threading communication
-(void) Performselectoronmainthread: (SEL) aselector withobject: (ID) arg waituntildone: (BOOL) wait; -(void) Performselector: (SEL) aselector onthread: (Nsthread *) thr Withobject: (ID) arg waituntildone :(BOOL) wait;
4. Example of inter-thread communication-Image download