1. Create and start a thread. An NSThread object represents a thread. Create and start a thread.
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];[thread start];
2. Thread-related usage main thread-related usage
1 + (NSThread *)mainThread;2 - (BOOL)isMainThread;3 + (BOOL)isMainThread;
Get current thread
1 NSThread *current = [NSThread currentThread];
Thread Scheduling Priority
1 + (double)threadPriority;2 + (BOOL)setThreadPriority:(double)p;3 - (double)threadPriority;4 - (BOOL)setThreadPriority:(double)p;
The scheduling priority value range is 0.0 ~ 1.0. The default value is 0.5. The higher the value, the higher the priority. The thread name.
1 - (void) setName:(NSString *)n;2 - (NSString *)name;
The thread is automatically started after the thread is created.
1 [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
Implicitly create and start a thread
1 [self performSelectorInBackground:@selector(run) withObject:nil];
3. Five states of the thread New (New) Ready (Runnable) Running (Running) blocking (Blocked) Dead (Dead) 4. Control the thread state to start the thread
1 - (void)start;
Thread Blocking
1 + (void)sleepUntilDate:(NSDate *)date;2 + (void)sleepForTimeInterval:(NSTimeInterval)ti;
Force stop thread
1 + (void)exit;
5. multi-thread security risk Resource Sharing: multiple threads access the same resource; Data disorder and data security problems are easily caused when processing shared data; 6. security risk solution-mutex lock format: @ synchroniazed (Lock Object) {code} Advantages and Disadvantages of mutex lock: advantage: effectively prevents data security problems caused by multi-thread resource snatch; disadvantages: a large amount of CPU resources are consumed; Prerequisite for mutex lock: multiple threads snatch the same resource; related terms: thread synchronization; meaning of thread synchronization: multiple threads execute tasks in sequence; the mutex lock uses the thread synchronization technology. 7. atomic attribute and non-atomic attribute OC have two options: nonatomic and atomic when defining attributes: atomic attribute, locking the setter method (atomic by default); nonatomic: Non-atomic attribute, the setter method will not be locked; the atomic Locking Principle:
1 @property (assign, atomic) int age;2 - (void)setAge:(int)age3 {4 @synchronized(self) {5 _age = age;6 }7 }Nonatomic compared with atomic nonatomic: thread security, which consumes a lot of resources; atomic: The thread is not secure and suitable for mobile devices with small memory. iOS development suggestions: all attributes are defined as nonatomic. Do not use multiple threads to snatch the same resource. Do your best to submit the lock and resource grabbing business logic to the server to reduce the pressure on the mobile client. inter-thread communication one thread transmits data to another thread; one thread completes the task and forwards it to another thread to continue executing the task; commonly used communication methods between threads
1 - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;2 - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;