Basic NSThread usage

Source: Internet
Author: User

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;

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.