iOS多線程---NSOperation介紹和使用

來源:互聯網
上載者:User

標籤:thread   nbsp   load   ber   cti   ios   control   cat   抽象   

1.  NSOperation實現多線程編程,需要和NSOperationQueue一起使用。

(1)先將要執行的操作封裝到NSOperation中

(2)將NSOperation對象添加到NSOperationQueue中

(3)系統將自動將NSOPeration從NSOperationQueue中取出來,放到一個新的線程中去執行

2.NSOperation 的子類

  NSOperation是一個抽象類別,並沒有封裝操作的能力。因此要用到它的子類:

      (1)NSInvocationOperation

  (2)NSBlockOperation

3. NSInvocationOperation 和NSBlockOperation  的建立

(1)NSInvocationOperation的建立

    NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testAction) object:nil];        [operation start];

2017-06-18 10:17:04.165 demo[17389:3000329] 現在的線程----<NSThread: 0x60000007bf80>{number = 1, name = main}

 註:操作對象預設在主線程中執行,只有加入到隊列中才會開啟新的線程。既預設情況下,如果操作沒有放到Queue中只是同步執行,只有放到了NSOperationQueue中才會非同步執行。

(2)NSBlockOperation的建立

(1)代碼1

    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{        NSLog(@"現在的線程----%@",[NSThread currentThread]);    }];        [operation start];

列印結果:

2017-06-18 10:20:19.440 demo[17409:3003510] 現在的線程----<NSThread: 0x6080000713c0>{number = 1, name = main}

 

(2)代碼2

- (void)viewDidLoad {    [super viewDidLoad];        NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{        NSLog(@"現在的線程----%@",[NSThread currentThread]);    }];        [operation addExecutionBlock:^{        NSLog(@"addExecution 的線程  ----%@",[NSThread currentThread]);    }];        [operation start];}

2017-06-18 10:26:31.545 demo[17436:3012476] 現在的線程----<NSThread: 0x608000079900>{number = 1, name = main}

2017-06-18 10:26:31.545 demo[17436:3012571] addExecution 的線程  ----<NSThread: 0x600000262f00>{number = 3, name = (null)}

 註:NSBlockOperation 封裝的運算元>1  那麼就會開啟新的線程。

 

4.NSOperationQueue 

NSOperationQueue是操作對象的隊列。將NSOperation對象放到NSOperationQueue裡,系統會自動取出NSOperation,開啟新線程 。

(1)NSOperationQueue添加操作的方式

    //定義隊列    NSOperationQueue *queue = [[NSOperationQueue alloc]init];    //將操作添加到隊列    [queue addOperation:operation];    [queue addOperation:operation1];    [queue addOperation:operation2];    //用block的方式添加操作    [queue addOperationWithBlock:^{        NSLog(@"現在的線程----%@",[NSThread currentThread]);    }];

(2)NSOperationQueue開啟線程的方式

////  ViewController.m//  demo////  Created by 登 on 2017/6/17.//  Copyright ? 2017年 登. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testAction) object:nil];        NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testAction1) object:nil];        NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{        NSLog(@"現在的線程----%@",[NSThread currentThread]);    }];        [operation2 addExecutionBlock:^{        NSLog(@"addExecution 的線程  ----%@",[NSThread currentThread]);    }];        //定義隊列    NSOperationQueue *queue = [[NSOperationQueue alloc]init];    //將操作添加到隊列    [queue addOperation:operation];    [queue addOperation:operation1];    [queue addOperation:operation2];    //用block的方式添加操作    [queue addOperationWithBlock:^{        NSLog(@"現在的線程----%@",[NSThread currentThread]);    }];    }-(void)testAction{    NSLog(@"現在的線程----%@",[NSThread currentThread]);}-(void)testAction1{    NSLog(@"現在的線程----%@",[NSThread currentThread]);}@end

2017-06-18 10:49:44.163 demo[17479:3030821] 現在的線程----<NSThread: 0x60000007c380>{number = 5, name = (null)}

2017-06-18 10:49:44.163 demo[17479:3030826] 現在的線程----<NSThread: 0x60000007c040>{number = 3, name = (null)}

2017-06-18 10:49:44.163 demo[17479:3030820] 現在的線程----<NSThread: 0x60000007c2c0>{number = 4, name = (null)}

2017-06-18 10:49:44.163 demo[17479:3030842] 現在的線程----<NSThread: 0x60000007c540>{number = 6, name = (null)}

2017-06-18 10:49:44.164 demo[17479:3030846] addExecution 的線程  ----<NSThread: 0x60800006ec40>{number = 7, name = (null)}

 註:

1.NSOPerationQueue將操作取出,每一個操作都會開啟一個新的線程。所有的操作都是非同步並發執行。

2.其中Operation2用NSBlockOPeration添加了兩個任務,這兩個任務開啟兩個線程,同樣並發執行。

 

iOS多線程---NSOperation介紹和使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.