多线程NSThread基本用法

 
 
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //开启多线程方法一
//    [self performSelectorInBackground:@selector(threadAction) withObject:nil];
   
    //开启多线程方法二
//    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(threadAction) object:nil];
    //thread.name = @"thread1";
//    NSLog(@"MAIN中:%@",thread);

//    [thread start];
   
    //开启多线程方法三
//    [NSThread detachNewThreadSelector:@selector(threadAction) toTarget:self withObject:nil];
   


   

    for (int i = 0; i < 50; i ++) {
        NSLog(@"main: %d",i);
    }
}

- (void)threadAction{
   
    NSThread *thread = [NSThread currentThread];
        NSLog(@"thread中:%@",thread);
   
    for (int i = 0 ; i < 50; i ++) {
        NSLog(@"thread:  %d",i);
       
        if (i==10) {
            [NSThread exit];
        }
       
        if ([NSThread isMultiThreaded]) {
            NSLog(@"是多线程");
        }
    }
}

@end
原文地址:https://www.cnblogs.com/chillytao-suiyuan/p/4834124.html