Original address: http://www.cnblogs.com/wendingding/p/3805119.html
1, Simple instructions for creating and starting threads
An NSThread object represents a thread
Create and start threads
(1) NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start];
//As soon as the thread starts, it will execute the self run method in the thread
Main thread related usage
(NSThread *)mainThread; / / get the main thread
(BOOL)isMainThread; / / whether it is the main thread
(BOOL)isMainThread; / / whether it is the main thread
Other uses
Get current thread
NSThread *current = [NSThread currentThread];
Scheduling priority of thread: the value range of scheduling priority is 0.0 ~ 1.0, and the default is 0.5. The larger the value is, the higher the priority is
(double)threadPriority;
(BOOL)setThreadPriority:(double)p;
Set the name of the thread
(void)setName:(NSString *)n;
(NSString *)name;
Other ways to create threads
(2) Automatically start the thread [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
(3) Implicitly create and start the thread [self performSelectorInBackground:@selector(run) withObject:nil];
Advantages and disadvantages of the above two ways to create threads
Advantages: simple and fast
Disadvantage: unable to set the thread in more detail
2, Code example
1. Create in an old way
//
// YYViewController.m
//
//
// Created by apple on 14-6-23.
// Copyright (c) 2014 itcase. All rights reserved
//
#import "YYViewController.h"
#import <pthread.h>
@interface YYViewController ()
- (IBAction)btnClick;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
//Button click event
- (IBAction)btnClick {
//1. Get the current thread
NSThread *current=[NSThread currentThread];
//Main thread
NSLog(@"btnClick----%@",current);
//2. Use for loop to perform some time-consuming operations
pthread_t thread;
pthread_create(&thread, NULL, run, NULL);
}
//c language function
void *run(void *data)
{
//Get the current thread, which is a newly created thread
NSThread *current=[NSThread currentThread];
for (int i=0; i<10000; i++) {
NSLog(@"btnClick---%d---%@",i,current);
}
return NULL;
}
//Multiple threads. When clicking the button to execute the method call, the main thread is not blocked
@end
Effect:
Print results:
2. Use NSThread to create thread
//
// YYViewController.m
//
//
// Created by apple on 14-6-23.
// Copyright (c) 2014 itcase. All rights reserved
//
#import "YYViewController.h"
#import <pthread.h>
@interface YYViewController ()
- (IBAction)btnClick;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
//Button click event
- (IBAction)btnClick {
//1. Get the current thread
NSThread *current=[NSThread currentThread];
//Main thread
NSLog(@"btnClick----%@",current);
//Another way to get the main thread
NSThread *main=[NSThread mainThread];
NSLog(@"Main thread-------%@",main);
//2. Perform some time-consuming operations
[self creatNSThread];
// [self creatNSThread2];
// [self creatNSThread3];
}
/**
* NSThread Create thread mode 1
* 1> Create initialization thread first
* 2> start Open thread
*/
-(void)creatNSThread
{
NSThread *thread=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"thread A"];
//Set a name for the thread
thread.name=@"thread A";
//Open thread
[thread start];
NSThread *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"thread B"];
//Set a name for the thread
thread2.name=@"thread B";
//Open thread
[thread2 start];
}
/**
* NSThread Create thread mode 2
*Start the thread directly (automatically) after creation
*/
-(void)creatNSThread2
{
// NSThread *thread=[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject: @ "start directly (automatically) after thread creation"];
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"After thread creation, directly(automatic)start-up"];
}
/**
* NSThread Create thread mode 3
* Implicitly create threads and start them directly (automatically)
*/
-(void)creatNSThread3
{
//Execute in background thread = = = execute in child thread
[self performSelectorInBackground:@selector(run:) withObject:@"Implicit creation"];
}
-(void)run:(NSString *)str
{
//Get current thread
NSThread *current=[NSThread currentThread];
//Printout
for (int i=0; i<10; i++) {
NSLog(@"run---%@---%@",current,str);
}
}
@end
Call thread 1, and the print result is:
Call thread 2
Call thread 3