IOS開發(61)之GCD執行非UI的操作

來源:互聯網
上載者:User

1 前言

當執行那些與 UI 無關的任務,或者與 UI 互動的任務時,和執行其他任務一樣,會需要大量時間,以上情況會經常出現。我們可以使用 dispatch_sync函數在一個指派隊列上執行同步任務。你必須做的事情就是提供一個此隊列的控制代碼了,這個隊列必須運行任務,並且一個代碼塊會在這個隊列上執行。 今天我們就來學習一下GCD執行非UI的操作。


2 代碼執行個體
TestDemo.h

 

[plain]
#import <Foundation/Foundation.h> 
 
@interface TestDemo : NSObject 
 
-(void)testMethod; 
-(void)testMethod2; 
@end 

#import <Foundation/Foundation.h>

@interface TestDemo : NSObject

-(void)testMethod;
-(void)testMethod2;
@end
TestDemo.m

 

[plain]
#import "TestDemo.h" 
 
@implementation TestDemo 
 
/**************Objc Method Start*********/ 
//Block Object 
void (^printFrom1To1000)(void) = ^{ 
    NSUInteger counter = 0; 
    for (counter = 1;counter <= 1000;counter++){ 
    NSLog(@"Counter = %lu - Thread = %@", 
          (unsigned long)counter, 
          [NSThread currentThread]); 
    } 
}; 
//測試方法 
-(void)testMethod{ 
    /* 
     Dispatch_get_global_queue 函數的第一個參數說明了並發隊列的優先順序,這個屬性 GCD 必須替程式員檢 
     索。優先順序越高,將提供更多的 CPU Timeslice 來擷取該隊列執行的代碼。 
     Dispatch_get_global_queue 函數的第一個參數:  
     DISPATCH_QUEUE_PRIORITY_LOW 
     您的任務比正常任務用到更少的 Timeslice。  
     DISPATCH_QUEUE_PRIORITY_DEFAULT 
     執行代碼的預設系統優先順序將應用於您的任務。 
     DISPATCH_QUEUE_PRIORITY_HIGH 
     和正常任務相比,更多的 Timeslices 會應用到你的任務中。 
     Dispatch_get_global_queue 函數的第二個參數已經儲存了,只要一直給它輸入數值 0 就可以了。 
     */ 
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_sync(concurrentQueue, printFrom1To1000); 
    dispatch_sync(concurrentQueue, printFrom1To1000); 

/**************Objc Method End*********/ 
 
/**************C Method Start*********/ 
//Block Object 
void print2From1To1000(void *paramContext){ 
    NSUInteger counter = 0; for (counter = 1;counter <= 1000;counter++){ 
        NSLog(@"Counter = %lu - Thread = %@", 
        (unsigned long)counter, [NSThread currentThread]); 
    } 

//測試方法 
-(void)testMethod2{ 
    /* 
      
     */ 
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_sync_f(concurrentQueue, NULL,print2From1To1000); 
    dispatch_sync_f(concurrentQueue, NULL,print2From1To1000); 

/**************C Method End*********/ 
@end 

#import "TestDemo.h"

@implementation TestDemo

/**************Objc Method Start*********/
//Block Object
void (^printFrom1To1000)(void) = ^{
    NSUInteger counter = 0;
    for (counter = 1;counter <= 1000;counter++){
    NSLog(@"Counter = %lu - Thread = %@",
          (unsigned long)counter,
          [NSThread currentThread]);
    }
};
//測試方法
-(void)testMethod{
    /*
     Dispatch_get_global_queue 函數的第一個參數說明了並發隊列的優先順序,這個屬性 GCD 必須替程式員檢
     索。優先順序越高,將提供更多的 CPU Timeslice 來擷取該隊列執行的代碼。
     Dispatch_get_global_queue 函數的第一個參數:
     DISPATCH_QUEUE_PRIORITY_LOW
     您的任務比正常任務用到更少的 Timeslice。
     DISPATCH_QUEUE_PRIORITY_DEFAULT
     執行代碼的預設系統優先順序將應用於您的任務。
     DISPATCH_QUEUE_PRIORITY_HIGH
     和正常任務相比,更多的 Timeslices 會應用到你的任務中。
     Dispatch_get_global_queue 函數的第二個參數已經儲存了,只要一直給它輸入數值 0 就可以了。
     */
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_sync(concurrentQueue, printFrom1To1000);
    dispatch_sync(concurrentQueue, printFrom1To1000);
}
/**************Objc Method End*********/

/**************C Method Start*********/
//Block Object
void print2From1To1000(void *paramContext){
    NSUInteger counter = 0; for (counter = 1;counter <= 1000;counter++){
        NSLog(@"Counter = %lu - Thread = %@",
        (unsigned long)counter, [NSThread currentThread]);
    }
}
//測試方法
-(void)testMethod2{
    /*
    
     */
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_sync_f(concurrentQueue, NULL,print2From1To1000);
    dispatch_sync_f(concurrentQueue, NULL,print2From1To1000);
}
/**************C Method End*********/
@end
main.m

 

[plain]
import <Foundation/Foundation.h> 
#import "TestDemo.h" 
 
int main(int argc, const char * argv[]) 

 
    @autoreleasepool { 
         
        TestDemo *test = [[TestDemo alloc] init]; 
//        [test testMethod]; 
        [test testMethod2]; 
        [test release]; 
         
    } 
    return 0; 

#import <Foundation/Foundation.h>
#import "TestDemo.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        TestDemo *test = [[TestDemo alloc] init];
//        [test testMethod];
        [test testMethod2];
        [test release];
       
    }
    return 0;
}
運行結果


2013-05-10 13:50:37.361 GCDExecuteNonUITest[556:303] Counter = 1 - Thread = <NSThread: 0x10010b2c0>{name = (null), num = 1}

2013-05-10 13:50:37.363 GCDExecuteNonUITest[556:303] Counter = 2 - Thread = <NSThread: 0x10010b2c0>{name = (null), num = 1}

...

2013-05-10 13:50:38.255 GCDExecuteNonUITest[556:303] Counter = 1 - Thread = <NSThread: 0x10010b2c0>{name = (null), num = 1}

2013-05-10 13:50:38.256 GCDExecuteNonUITest[556:303] Counter = 2 - Thread = <NSThread: 0x10010b2c0>{name = (null), num = 1}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.