NSThread的基本使用

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   // [self thread1];
   // [self thread2];
    [self thread3];
}
//第三种种创建线程的方式
- (void)thread3
{
    [self performSelectorInBackground:@selector(test:) withObject:@"aa"];
}

//第二种创建线程的方式
- (void)thread2
{
    [NSThread detachNewThreadSelector:@selector(test:) toTarget:self withObject:@"11111"];
}
//第一种创建线程的方式
- (void)thread1
{
    NSThread *th = [[NSThread alloc] initWithTarget:self selector:@selector(test:) object:nil];
    th.name = @"测试的线程";
    [th start];
}

- (void)test:(id)arg
{
    NSLog(@"线程名%@",[NSThread currentThread]);
    if (arg) {
        NSLog(@"参数-%@",arg);
    }
    
}

@end


你可能感兴趣的:(NSThread的基本使用)