Objective-C中的多线程之NSThread

使用NSThread有三种创建进程的方式:

1) 创建一个新的进程,需要运行start才能启动 

 NSThread *newThread = [NSThread alloc]initWithTarget:<#(nonnull id)#> selector:<#(nonnull SEL)#> object:<#(nullable id)#>];

例如:

 NSThread *threadA = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"PasswingA"];

[threadA start]


2) 创建一个进程,可以直接启动,无需运行start

 [NSThread detachNewThreadSelector:<#(nonnull SEL)#> toTarget:<#(nonnull id)#> withObject:<#(nullable id)#>];

例如:

[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"PasswingC"];


3) 后台运行的进程

 [self performSelectorInBackground:<#(nonnull SEL)#> withObject:<#(nullable id)#>];

 例如:

[self performSelectorInBackground:@selector(run:) withObject:@"PasswingD"];


#import "ViewController.h"

@interface ViewController ()
- (IBAction)createThreadA:(UIButton *)sender;

@end

@implementation ViewController

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)run:(NSString *)param {
    
    NSThread *newThread = [NSThread currentThread];
    for(int i = 0; i < 10; i++){
        NSLog(@"The new Thread is : %@, Param: is %@", newThread, param);
    }
}

- (IBAction)createThreadA:(UIButton *)sender {
    //获取当前线程
    NSThread *current = [NSThread currentThread];
    NSLog(@"Current thread is: %@", current);
    //获取主线程
    NSThread *main = [NSThread mainThread];
    NSLog(@"Main thread is %@", main);
    
    
    if([sender.currentTitle isEqualToString:@"创建线程A"]){
         NSLog(@"This is button A");
        //创建一个新的线程A
        NSThread *threadA = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"PasswingA"];
        threadA.name = @"Thread A";
        [threadA start];
    }
    if([sender.currentTitle isEqualToString:@"创建线程B"]){
        NSLog(@"This is button B");
        //创建一个新的线程B
        NSThread *threadB = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"PasswringB"];
        threadB.name = @"Thread B";
        [threadB start];
    }
    if([sender.currentTitle isEqualToString:@"创建线程C"]){
        NSLog(@"This is button C");
        //创建一个新的线程C
        [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"PasswingC"];
    }
    if([sender.currentTitle isEqualToString:@"创建线程D"]){
        NSLog(@"This is button D");
        //创建一个新的线程D
        [self performSelectorInBackground:@selector(run:) withObject:@"PasswingD"];
        
    }
    
    
}

@end

生成的log数据如下:

2018-03-25 21:41:49.313867+0800 TOCThreadc[229:5350532] [MC] Lazy loading NSBundle MobileCoreServices.framework

2018-03-25 21:41:49.315489+0800 TOCThreadc[229:5350532] [MC] Loaded MobileCoreServices.framework

2018-03-25 21:42:50.102034+0800 TOCThreadc[229:5350532] Current thread is: {number = 1, name = main}

2018-03-25 21:42:50.102395+0800 TOCThreadc[229:5350532] Main thread is {number = 1, name = main}

2018-03-25 21:42:50.102600+0800 TOCThreadc[229:5350532] This is button A

2018-03-25 21:42:50.103854+0800 TOCThreadc[229:5354269] The new Thread is : {number = 3, name = Thread A}, Param: is PasswingA

2018-03-25 21:42:50.104431+0800 TOCThreadc[229:5354269] The new Thread is : {number = 3, name = Thread A}, Param: is PasswingA

2018-03-25 21:42:50.105203+0800 TOCThreadc[229:5354269] The new Thread is : {number = 3, name = Thread A}, Param: is PasswingA

2018-03-25 21:42:50.105489+0800 TOCThreadc[229:5354269] The new Thread is : {number = 3, name = Thread A}, Param: is PasswingA

2018-03-25 21:42:50.106255+0800 TOCThreadc[229:5354269] The new Thread is : {number = 3, name = Thread A}, Param: is PasswingA

2018-03-25 21:42:50.107416+0800 TOCThreadc[229:5354269] The new Thread is : {number = 3, name = Thread A}, Param: is PasswingA

2018-03-25 21:42:50.107658+0800 TOCThreadc[229:5354269] The new Thread is : {number = 3, name = Thread A}, Param: is PasswingA

2018-03-25 21:42:50.108121+0800 TOCThreadc[229:5354269] The new Thread is : {number = 3, name = Thread A}, Param: is PasswingA

2018-03-25 21:42:50.108519+0800 TOCThreadc[229:5354269] The new Thread is : {number = 3, name = Thread A}, Param: is PasswingA

2018-03-25 21:42:50.108851+0800 TOCThreadc[229:5354269] The new Thread is : {number = 3, name = Thread A}, Param: is PasswingA

2018-03-25 21:45:04.884265+0800 TOCThreadc[229:5350532] Current thread is: {number = 1, name = main}

2018-03-25 21:45:04.884798+0800 TOCThreadc[229:5350532] Main thread is {number = 1, name = main}

2018-03-25 21:45:04.885166+0800 TOCThreadc[229:5350532] This is button B

2018-03-25 21:45:04.886779+0800 TOCThreadc[229:5360984] The new Thread is : {number = 4, name = Thread B}, Param: is PasswringB

2018-03-25 21:45:04.887598+0800 TOCThreadc[229:5360984] The new Thread is : {number = 4, name = Thread B}, Param: is PasswringB

2018-03-25 21:45:04.887801+0800 TOCThreadc[229:5360984] The new Thread is : {number = 4, name = Thread B}, Param: is PasswringB

2018-03-25 21:45:04.887990+0800 TOCThreadc[229:5360984] The new Thread is : {number = 4, name = Thread B}, Param: is PasswringB

2018-03-25 21:45:04.888187+0800 TOCThreadc[229:5360984] The new Thread is : {number = 4, name = Thread B}, Param: is PasswringB

2018-03-25 21:45:04.888609+0800 TOCThreadc[229:5360984] The new Thread is : {number = 4, name = Thread B}, Param: is PasswringB

2018-03-25 21:45:04.888750+0800 TOCThreadc[229:5360984] The new Thread is : {number = 4, name = Thread B}, Param: is PasswringB

2018-03-25 21:45:04.888928+0800 TOCThreadc[229:5360984] The new Thread is : {number = 4, name = Thread B}, Param: is PasswringB

2018-03-25 21:45:04.889071+0800 TOCThreadc[229:5360984] The new Thread is : {number = 4, name = Thread B}, Param: is PasswringB

2018-03-25 21:45:04.889484+0800 TOCThreadc[229:5360984] The new Thread is : {number = 4, name = Thread B}, Param: is PasswringB

2018-03-25 21:45:13.373683+0800 TOCThreadc[229:5350532] Current thread is: {number = 1, name = main}

2018-03-25 21:45:13.374940+0800 TOCThreadc[229:5350532] Main thread is {number = 1, name = main}

2018-03-25 21:45:13.375207+0800 TOCThreadc[229:5350532] This is button C

2018-03-25 21:45:13.376183+0800 TOCThreadc[229:5361505] The new Thread is : {number = 5, name = (null)}, Param: is PasswingC

2018-03-25 21:45:13.376873+0800 TOCThreadc[229:5361505] The new Thread is : {number = 5, name = (null)}, Param: is PasswingC

2018-03-25 21:45:13.377982+0800 TOCThreadc[229:5361505] The new Thread is : {number = 5, name = (null)}, Param: is PasswingC

2018-03-25 21:45:13.378179+0800 TOCThreadc[229:5361505] The new Thread is : {number = 5, name = (null)}, Param: is PasswingC

2018-03-25 21:45:13.378663+0800 TOCThreadc[229:5361505] The new Thread is : {number = 5, name = (null)}, Param: is PasswingC

2018-03-25 21:45:13.378973+0800 TOCThreadc[229:5361505] The new Thread is : {number = 5, name = (null)}, Param: is PasswingC

2018-03-25 21:45:13.379128+0800 TOCThreadc[229:5361505] The new Thread is : {number = 5, name = (null)}, Param: is PasswingC

2018-03-25 21:45:13.379720+0800 TOCThreadc[229:5361505] The new Thread is : {number = 5, name = (null)}, Param: is PasswingC

2018-03-25 21:45:13.380733+0800 TOCThreadc[229:5361505] The new Thread is : {number = 5, name = (null)}, Param: is PasswingC

2018-03-25 21:45:13.381088+0800 TOCThreadc[229:5361505] The new Thread is : {number = 5, name = (null)}, Param: is PasswingC

2018-03-25 21:45:24.641283+0800 TOCThreadc[229:5350532] Current thread is: {number = 1, name = main}

2018-03-25 21:45:24.642197+0800 TOCThreadc[229:5350532] Main thread is {number = 1, name = main}

2018-03-25 21:45:24.642473+0800 TOCThreadc[229:5350532] This is button D

2018-03-25 21:45:24.644366+0800 TOCThreadc[229:5362011] The new Thread is : {number = 6, name = (null)}, Param: is PasswingD

2018-03-25 21:45:24.645737+0800 TOCThreadc[229:5362011] The new Thread is : {number = 6, name = (null)}, Param: is PasswingD

2018-03-25 21:45:24.646657+0800 TOCThreadc[229:5362011] The new Thread is : {number = 6, name = (null)}, Param: is PasswingD

2018-03-25 21:45:24.647678+0800 TOCThreadc[229:5362011] The new Thread is : {number = 6, name = (null)}, Param: is PasswingD

2018-03-25 21:45:24.648481+0800 TOCThreadc[229:5362011] The new Thread is : {number = 6, name = (null)}, Param: is PasswingD

2018-03-25 21:45:24.648762+0800 TOCThreadc[229:5362011] The new Thread is : {number = 6, name = (null)}, Param: is PasswingD

2018-03-25 21:45:24.649515+0800 TOCThreadc[229:5362011] The new Thread is : {number = 6, name = (null)}, Param: is PasswingD

2018-03-25 21:45:24.649897+0800 TOCThreadc[229:5362011] The new Thread is : {number = 6, name = (null)}, Param: is PasswingD

2018-03-25 21:45:24.650917+0800 TOCThreadc[229:5362011] The new Thread is : {number = 6, name = (null)}, Param: is PasswingD

2018-03-25 21:45:24.651378+0800 TOCThreadc[229:5362011] The new Thread is : {number = 6, name = (null)}, Param: is PasswingD


你可能感兴趣的:(OC)