GCD의 일반적인 사용(단일 예/다중 스레드/1)

1561 단어
오늘 디스패치를 배웠어요.
다음은 GCD 스레드의 기본 사용을 적으십시오. (단일 예는 뒤에 삽입됩니다.)
/**
 *  GCD        Grand Central Dispatch
 *
 *      ,       
 */
// 1.     ,   [NSOperationQueue mainQueue]
// 2.      ,     ,   
// 3.       , DISPATCH_QUEUE_SERIAL   
      //           DISPATCH_QUEUE_CONCURRENT   
  
    //          ,               
       - (void)printNumber:(NSString *)name {
              for (int i = 0 ; i < 100; i++) {
              NSLog(@"%d,     :%@", i, [NSThread currentThread]);
              NSLog(@"%@", name);
            }
    }



//        
    dispatch_queue_t myQueue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_PRIORITY_DEFAULT);

//      ,   /   
//    dispath_async (  ,   block);             
//    dispath_sync (  ,   block);        
//     
dispatch_async(myQueue, ^{
    [self printNumber:@"GCD"];
    [self printNumber:@"G1"];
});

//     
dispatch_async(myQueue, ^{
    [self printNumber:@"G2"];
});

         :   GCD,        G1,G1       G2


//     
dispatch_queue_t conQueue = dispatch_queue_create("conQueue", DISPATCH_QUEUE_CONCURRENT);//f
dispatch_async(conQueue, ^{
    [self printNumber:@"G1"];
    [self printNumber:@"G2"];
});
dispatch_async(conQueue, ^{
    [self printNumber:@"G3"];
});
dispatch_async(conQueue, ^{
    [self printNumber:@"G4"];
});
       :       G1 G3 G4 ,       G2.

좋은 웹페이지 즐겨찾기