[IOS Multithreading & Networking-1.1]-multithreaded Nsthread

Source: Internet
Author: User

Basic use of A.nsthread 1. Creating and Starting ThreadsA Nsthread object represents a thread

Creating, starting Threads
Nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil];
[Thread start];
When a thread is started, the run method of self is executed in threads thread

Main thread related usage
+ (Nsthread *) Mainthread; Get the main thread
-(BOOL) Ismainthread; is the main thread
+ (BOOL) Ismainthread; is the main thread2. Other methodsGet current thread
Nsthread *current = [Nsthread CurrentThread];

Scheduling Priority for Threads
+ (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

The name of the thread
-(void) SetName: (NSString *) n;
-(NSString *) name;3. Other ways to create threadsAutomatically start a thread after creating a thread
[Nsthread detachnewthreadselector: @selector (Run) totarget:self Withobject:nil];

Implicitly creating and starting a thread
[Self Performselectorinbackground: @selector (Run) Withobject:nil];

Pros and cons of the above 2 ways to create threads
Advantages: Simple and fast
Cons: Unable to set the thread in more detail4. Thread Life cycle5. Control thread TurntableStart thread
-(void) start;
Go to Ready status, run status. Automatically enters the death state when the thread task is completed

Blocking (pausing) threads
+ (void) Sleepuntildate: (NSDate *) date;
+ (void) Sleepfortimeinterval: (Nstimeinterval) ti;
Enter blocking state

Force Stop Thread
+ (void) exit;
Into the state of death

Note: Once the thread has stopped (dead), it cannot be opened again.6. Multi-threaded security risksResource sharing
1 of resources may be shared by multiple threads, that is, multiple threads may access the same block 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 resource7. Mutex (to prevent multi-threaded resource looting) (1) The use of the mutex lock format
@synchronized (Lock Object) {//code to be locked}
Note: Locking 1 parts of code with only 1 locks, with multiple locks is not valid

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

Use of mutexes: Multiple threads rob the same piece of resources

Related Professional terminology: Thread synchronization
Thread synchronization means that multiple threads perform tasks sequentially
Mutex is the use of thread synchronization technology(2) Locking @synchronized@property (nonatomic, strong) NSObject *lock;   Self.lock = [[NSObject alloc] init] @synchronized (self.lock) {...} Note: To use the same "lock" to properly lock the code resources advantages: effectively prevent multi-threaded snatch resource disadvantage: Consume a lot of CPU resources In fact, the member property modifier Atomic Lock, which is the default, so we generally use nonatomic, improve performance as far as possible to avoid multi-threaded snatch resources to the server side to handle the processing(3) about atomic and nonatomic modifiersOC has nonatomic and atomic two choices when defining attributes
Atomic: Atomic attribute, locking for setter method (default is Atomic)
Nonatomic: Non-atomic attribute, does not lock the setter method

Atomic Plus Lock principle
@property (assign, atomic) int age;
-(void) Setage: (int) Age
{
@synchronized (self) {
_age = age;
}
}(4) Atomic vs NonatomicComparison of Nonatomic and atomic
Atomic: Thread-safe, requires a lot of resources
Nonatomic: Non-thread-safe, suitable for small memory mobile devices

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 clients8. Inter-thread communicationWhat is called inter-thread communication
In 1 processes, threads often do not exist in isolation and require frequent communication between multiple threads

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

Common methods for inter-thread communication
-(void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait;
-(void) Performselector: (SEL) aselector onthread: (Nsthread *) THR Withobject: (ID) arg waituntildone: (BOOL) wait;

[IOS Multithreading & Networking-1.1]-multithreaded Nsthread

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.