代码如下,非常简单:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//方式1 创建线程,
NSThread *st = [[NSThread alloc]initWithTarget:self selector:@selector(demo) object:nil];
//启动线程
[st start];
//方式2创建线程
[NSThread detachNewThreadSelector:@selector(demo) toTarget:self withObject:nil];
//方式3创建线程
[self performSelectorInBackground:@selector(demo) withObject:nil];
//方式4创建线程,带参数
NSThread *nst = [[NSThread alloc]initWithTarget:self selector:@selector(test:) object:@"abcderf"];
[nst start];
}
- (void)demo {
NSLog(@"我是子线成1");
}
- (void)test:(NSString *)str {
NSLog(@"str=%@",str);
}
@end