IOS线程学习(一)
1.NSThread?
官方的描述
An?NSThread?object controls a thread of execution. Use this class when you want to have an Objective-C method run in its own thread of execution. Threads are especially useful when you need to perform a lengthy task, but don’t want it to block the execution of the rest of the application.
NSThread能控制一個線程的執行, 當你想在自己的線程執行OC方法時請用此類。對于執行較長的任務時這是也很有用的,不會堵住程序里面剩下需要執行的任務。
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:NSSelectorFromString(@"myThread:") object:nil];//啟動線程 [thread start];//停止線程
//if (![thread isCancelled]) { //[thread cancel];
//? ? }
1 -(void)myThread:(id)sender{ 2 NSLog(@"%@" , sender); 3 @synchronized(self){ 4 while (true) { 5 [NSThread sleepForTimeInterval:1]; 6 static int i = 0; 7 NSLog(@"%i" , i++); 8 if (i == 10) { 9 [NSThread exit]; 10 } 11 } 12 } 13 } View Code結果:只打印到3時線程就終止了
:
也可以用這個方法啟動一個線程,但是不能是默認的Thread配置
[NSThread detachNewThreadSelector:NSSelectorFromString(@"myThread:") toTarget:self withObject:@"myThread"];也等同于,這個方法在NSObject中被定義,只要是繼承NSObject都可以這樣用
[self performSelectorInBackground:NSSelectorFromString(@"myThread:") withObject:@"myThread"];?2.NSOperation
目前我的理解就是一個封裝操作某操作的,然后調用其start方法,就在主線程執行!!!
如果不在主線程執行可以創建一個NSOperationQueue,然后將操作加入到其中執行
其有兩個子類NSBlockOperation和NSInvocationOperation
NSBlockOperation
The?NSBlockOperation?class is a concrete subclass of?NSOperation?that manages the concurrent execution of one or more blocks. You can use this object to execute several blocks at once without having to create separate operation objects for each. When executing more than one block, the operation itself is considered finished only when all blocks have finished executing.
?可見,NSBlockOperation是管理多個Block塊的,而且只有所有的Block都執行完了才會變成finished狀態;
NSLog(@"%@ mainT = " ,[NSThread currentThread]);NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{for (int i = 0; i<5; i++) {[NSThread sleepForTimeInterval:1];NSLog(@"block1>>>%i thread = %@" , i , [NSThread currentThread]);}}];[blockOp addExecutionBlock:^{for (int i = 0; i<10; i++) {[NSThread sleepForTimeInterval:1];NSLog(@"block2>>>%i thread = %@" , i , [NSThread currentThread]);}}];[blockOp start];NSLog(@"到這了");運行結果:一個Block就在主線程,多個就會并行執行其他block
NSInvocationOperation
The?NSInvocationOperation?class is a concrete subclass of?NSOperation?that manages the execution of a single encapsulated task specified as an invocation. You can use this class to initiate an operation that consists of invoking a selector on a specified object. This class implements a non-concurrent operation.
?可見,只能通過Action-Target模式加入一個操作
NSLog(@"%@ mainThread = " ,[NSThread currentThread]);NSInvocationOperation *invocationOperation1= [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(myOperation:) object:@"invocationOperation1"];[invocationOperation1 start];NSLog(@"到這了");-(void)myOperation:(id)sender{static int i = 0;while (i<4) {[NSThread sleepForTimeInterval:1];[NSThread isMainThread];NSLog(@"我是線程%@ %i", [NSThread currentThread] , i++);} }運行結果:
2015-12-19 22:57:23.038 MYThread[4942:44405] <NSThread: 0x7ffcba50c190>{number = 1, name = main} mainThread = 2015-12-19 22:57:24.043 MYThread[4942:44405] 我是線程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 0 2015-12-19 22:57:25.046 MYThread[4942:44405] 我是線程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 1 2015-12-19 22:57:26.051 MYThread[4942:44405] 我是線程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 2 2015-12-19 22:57:27.053 MYThread[4942:44405] 我是線程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 3 2015-12-19 22:57:27.054 MYThread[4942:44405] 到這了?3.NSOperationQueue
NSOperationQueue *queue = [[NSOperationQueue alloc] init];NSInvocationOperation *op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(method1) object:nil];[queue addOperation:op];NSLog(@"到這了!");?結果:
2016-03-17 09:17:22.710 ViewAnim[580:12599] 到這了! 2016-03-17 09:17:23.779 ViewAnim[580:12871] 111111 2016-03-17 09:17:24.853 ViewAnim[580:12871] 111111 2016-03-17 09:17:25.926 ViewAnim[580:12871] 111111 2016-03-17 09:17:27.001 ViewAnim[580:12871] 111111 2016-03-17 09:17:28.070 ViewAnim[580:12871] 111111 2016-03-17 09:17:29.143 ViewAnim[580:12871] 111111 。。。。。?
轉載于:https://www.cnblogs.com/pigface/p/5056233.html
總結
以上是生活随笔為你收集整理的IOS线程学习(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BZOJ-1024 生日快乐 D
- 下一篇: Arcgis Server发布服务