Multi-thread development-NSThrea and multi-thread nsthrea
Create and start
Create a thread first, and then start
// Create
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:nil];
// Start
[thread start];
Create and start[NSThread detachNewThreadSelector: @ selector (run :) toTarget: self withObject: nil];
Implicitly create and start
[Self initialize mselectorinbackground: @ selector (run :) withObject: @ "mj"];
Other methods
Execute operations on the specified Thread
[Self defined mselector: @ selector (run) onThread: thread withObject: nil waitUntilDone: YES];
The code above indicates that the self run method is called on the thread.
The final YES indicates that the above Code will be blocked, and the above Code will pass only after the run method is executed in the thread.
Perform operations on the main thread
[Self defined mselecw.mainthread: @ selector (run) withObject: nil waitUntilDone: YES];
Execute operations in the current thread
[Self defined mselector: @ selector (run) withObject: nil];
// Cancel the thread
- (void)cancel;
// Start the thread
- (void)start;
// Attributes used to determine the status of a thread
@property (readonly, getter=isExecuting) BOOL executing;
@property (readonly, getter=isFinished) BOOL finished;
@property (readonly, getter=isCancelled) BOOL cancelled;
// Set and obtain the thread name
-(void)setName:(NSString *)n;
-(NSString *)name;
// Obtain information about the current thread
+ (NSThread *)currentThread;
// Obtain master thread Information
+ (NSThread *)mainThread;
// Pause the current thread for a period of time or wait for a period of time
+ (void)sleepForTimeInterval:(NSTimeInterval)time;
+ (void)sleepUntilDate:(NSDate *)date;
Advantages and disadvantages:Advantage: NSThread is lighter than other multi-threaded solutions, allowing you to control thread objects more intuitively. Disadvantages: You need to manage the thread lifecycle and synchronize the thread. Thread Synchronization locks data with a certain amount of system overhead