Objective-C中的多執行緒之NSThread
使用NSThread有三種建立程序的方式:
1) 建立一個新的程序,需要執行start才能啟動
NSThread *newThread = [NSThread alloc]initWithTarget:<#(nonnull id)#> selector:<#(nonnull SEL)#> object:<#(nullable id)#>];
例如:NSThread *threadA = [[NSThreadalloc]initWithTarget:selfselector:@selector(run:) object:@"PasswingA"];
[threadA start]
2) 建立一個程序,可以直接啟動,無需執行start
[NSThread detachNewThreadSelector:<#(nonnull SEL)#> toTarget:<#(nonnull id)#> withObject:<#(nullable id)#>];
例如:[NSThreaddetachNewThreadSelector:@selector(run:) toTarget:selfwithObject:@"PasswingC"];
3) 後臺執行的程序
[self performSelectorInBackground:<#(nonnull SEL)#> withObject:<#(nullable id)#>];
[selfperformSelectorInBackground:@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: <NSThread: 0x600000068f40>{number = 1, name = main}
2018-03-25 21:42:50.102395+0800 TOCThreadc[229:5350532] Main thread is <NSThread: 0x600000068f40>{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 : <NSThread: 0x60400046c080>{number = 3, name = Thread A}, Param: is PasswingA
2018-03-25 21:42:50.104431+0800 TOCThreadc[229:5354269] The new Thread is : <NSThread: 0x60400046c080>{number = 3, name = Thread A}, Param: is PasswingA
2018-03-25 21:42:50.105203+0800 TOCThreadc[229:5354269] The new Thread is : <NSThread: 0x60400046c080>{number = 3, name = Thread A}, Param: is PasswingA
2018-03-25 21:42:50.105489+0800 TOCThreadc[229:5354269] The new Thread is : <NSThread: 0x60400046c080>{number = 3, name = Thread A}, Param: is PasswingA
2018-03-25 21:42:50.106255+0800 TOCThreadc[229:5354269] The new Thread is : <NSThread: 0x60400046c080>{number = 3, name = Thread A}, Param: is PasswingA
2018-03-25 21:42:50.107416+0800 TOCThreadc[229:5354269] The new Thread is : <NSThread: 0x60400046c080>{number = 3, name = Thread A}, Param: is PasswingA
2018-03-25 21:42:50.107658+0800 TOCThreadc[229:5354269] The new Thread is : <NSThread: 0x60400046c080>{number = 3, name = Thread A}, Param: is PasswingA
2018-03-25 21:42:50.108121+0800 TOCThreadc[229:5354269] The new Thread is : <NSThread: 0x60400046c080>{number = 3, name = Thread A}, Param: is PasswingA
2018-03-25 21:42:50.108519+0800 TOCThreadc[229:5354269] The new Thread is : <NSThread: 0x60400046c080>{number = 3, name = Thread A}, Param: is PasswingA
2018-03-25 21:42:50.108851+0800 TOCThreadc[229:5354269] The new Thread is : <NSThread: 0x60400046c080>{number = 3, name = Thread A}, Param: is PasswingA
2018-03-25 21:45:04.884265+0800 TOCThreadc[229:5350532] Current thread is: <NSThread: 0x600000068f40>{number = 1, name = main}
2018-03-25 21:45:04.884798+0800 TOCThreadc[229:5350532] Main thread is <NSThread: 0x600000068f40>{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 : <NSThread: 0x604000271a40>{number = 4, name = Thread B}, Param: is PasswringB
2018-03-25 21:45:04.887598+0800 TOCThreadc[229:5360984] The new Thread is : <NSThread: 0x604000271a40>{number = 4, name = Thread B}, Param: is PasswringB
2018-03-25 21:45:04.887801+0800 TOCThreadc[229:5360984] The new Thread is : <NSThread: 0x604000271a40>{number = 4, name = Thread B}, Param: is PasswringB
2018-03-25 21:45:04.887990+0800 TOCThreadc[229:5360984] The new Thread is : <NSThread: 0x604000271a40>{number = 4, name = Thread B}, Param: is PasswringB
2018-03-25 21:45:04.888187+0800 TOCThreadc[229:5360984] The new Thread is : <NSThread: 0x604000271a40>{number = 4, name = Thread B}, Param: is PasswringB
2018-03-25 21:45:04.888609+0800 TOCThreadc[229:5360984] The new Thread is : <NSThread: 0x604000271a40>{number = 4, name = Thread B}, Param: is PasswringB
2018-03-25 21:45:04.888750+0800 TOCThreadc[229:5360984] The new Thread is : <NSThread: 0x604000271a40>{number = 4, name = Thread B}, Param: is PasswringB
2018-03-25 21:45:04.888928+0800 TOCThreadc[229:5360984] The new Thread is : <NSThread: 0x604000271a40>{number = 4, name = Thread B}, Param: is PasswringB
2018-03-25 21:45:04.889071+0800 TOCThreadc[229:5360984] The new Thread is : <NSThread: 0x604000271a40>{number = 4, name = Thread B}, Param: is PasswringB
2018-03-25 21:45:04.889484+0800 TOCThreadc[229:5360984] The new Thread is : <NSThread: 0x604000271a40>{number = 4, name = Thread B}, Param: is PasswringB
2018-03-25 21:45:13.373683+0800 TOCThreadc[229:5350532] Current thread is: <NSThread: 0x600000068f40>{number = 1, name = main}
2018-03-25 21:45:13.374940+0800 TOCThreadc[229:5350532] Main thread is <NSThread: 0x600000068f40>{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 : <NSThread: 0x60000026cd00>{number = 5, name = (null)}, Param: is PasswingC
2018-03-25 21:45:13.376873+0800 TOCThreadc[229:5361505] The new Thread is : <NSThread: 0x60000026cd00>{number = 5, name = (null)}, Param: is PasswingC
2018-03-25 21:45:13.377982+0800 TOCThreadc[229:5361505] The new Thread is : <NSThread: 0x60000026cd00>{number = 5, name = (null)}, Param: is PasswingC
2018-03-25 21:45:13.378179+0800 TOCThreadc[229:5361505] The new Thread is : <NSThread: 0x60000026cd00>{number = 5, name = (null)}, Param: is PasswingC
2018-03-25 21:45:13.378663+0800 TOCThreadc[229:5361505] The new Thread is : <NSThread: 0x60000026cd00>{number = 5, name = (null)}, Param: is PasswingC
2018-03-25 21:45:13.378973+0800 TOCThreadc[229:5361505] The new Thread is : <NSThread: 0x60000026cd00>{number = 5, name = (null)}, Param: is PasswingC
2018-03-25 21:45:13.379128+0800 TOCThreadc[229:5361505] The new Thread is : <NSThread: 0x60000026cd00>{number = 5, name = (null)}, Param: is PasswingC
2018-03-25 21:45:13.379720+0800 TOCThreadc[229:5361505] The new Thread is : <NSThread: 0x60000026cd00>{number = 5, name = (null)}, Param: is PasswingC
2018-03-25 21:45:13.380733+0800 TOCThreadc[229:5361505] The new Thread is : <NSThread: 0x60000026cd00>{number = 5, name = (null)}, Param: is PasswingC
2018-03-25 21:45:13.381088+0800 TOCThreadc[229:5361505] The new Thread is : <NSThread: 0x60000026cd00>{number = 5, name = (null)}, Param: is PasswingC
2018-03-25 21:45:24.641283+0800 TOCThreadc[229:5350532] Current thread is: <NSThread: 0x600000068f40>{number = 1, name = main}
2018-03-25 21:45:24.642197+0800 TOCThreadc[229:5350532] Main thread is <NSThread: 0x600000068f40>{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 : <NSThread: 0x604000275000>{number = 6, name = (null)}, Param: is PasswingD
2018-03-25 21:45:24.645737+0800 TOCThreadc[229:5362011] The new Thread is : <NSThread: 0x604000275000>{number = 6, name = (null)}, Param: is PasswingD
2018-03-25 21:45:24.646657+0800 TOCThreadc[229:5362011] The new Thread is : <NSThread: 0x604000275000>{number = 6, name = (null)}, Param: is PasswingD
2018-03-25 21:45:24.647678+0800 TOCThreadc[229:5362011] The new Thread is : <NSThread: 0x604000275000>{number = 6, name = (null)}, Param: is PasswingD
2018-03-25 21:45:24.648481+0800 TOCThreadc[229:5362011] The new Thread is : <NSThread: 0x604000275000>{number = 6, name = (null)}, Param: is PasswingD
2018-03-25 21:45:24.648762+0800 TOCThreadc[229:5362011] The new Thread is : <NSThread: 0x604000275000>{number = 6, name = (null)}, Param: is PasswingD
2018-03-25 21:45:24.649515+0800 TOCThreadc[229:5362011] The new Thread is : <NSThread: 0x604000275000>{number = 6, name = (null)}, Param: is PasswingD
2018-03-25 21:45:24.649897+0800 TOCThreadc[229:5362011] The new Thread is : <NSThread: 0x604000275000>{number = 6, name = (null)}, Param: is PasswingD
2018-03-25 21:45:24.650917+0800 TOCThreadc[229:5362011] The new Thread is : <NSThread: 0x604000275000>{number = 6, name = (null)}, Param: is PasswingD
2018-03-25 21:45:24.651378+0800 TOCThreadc[229:5362011] The new Thread is : <NSThread: 0x604000275000>{number = 6, name = (null)}, Param: is PasswingD