UI - GCD


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


//GCD            ,                 ,      C    ,        ,     .
//GCD           ,      serial queue (    )   concurrent queue(    ).


//    (serial queue)
- (IBAction)serialQueueBt:(id)sender {
    //    :                    
    
    //    
    //1.           
//    dispatch_queue_t systemSerial = dispatch_get_main_queue();
//    
//    //          ,    dispatch_async        ,            ,         async   sync       ,        .
//               ,  dispatch_async     ,     ,    dispatch_sync      ,      ,     .
//    
//    dispatch_async(systemSerial, ^{
//        NSLog(@"11111111Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
//    });
//    
//    dispatch_async(systemSerial, ^{
//        NSLog(@"22222222Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
//    });
//    
//    dispatch_async(systemSerial, ^{
//        NSLog(@"33333333Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
//    });
//    
//    dispatch_async(systemSerial, ^{
//        NSLog(@"44444444Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
//    });
    
   //2.        
    //                ,          DISPATCH_QUEUE_SERIAL    ,DISPATCH_QUEUE_CONCURRENT    
    dispatch_queue_t maSerial = dispatch_queue_create("com.lanou3g.GJS150101", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(maSerial, ^{
        NSLog(@"11111111Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
    });
    
    dispatch_async(maSerial, ^{
        NSLog(@"22222222Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
    });
    
    dispatch_async(maSerial, ^{
        NSLog(@"33333333Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
    });
    
    dispatch_async(maSerial, ^{
        NSLog(@"44444444Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
    });
    
}
//    (concurrent queue)
- (IBAction)concurrentQueueBt:(id)sender {
    //     (concurrent queue) ,           .     ,           ,        .
    //1.         
    //       ,             ,          ,      ,  0;
//    dispatch_queue_t systemConcurrent = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

//    dispatch_async(systemConcurrent, ^{
//        NSLog(@"11111111Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
//    });
//    
//    dispatch_async(systemConcurrent, ^{
//        NSLog(@"22222222Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
//    });
//    
//    dispatch_async(systemConcurrent, ^{
//        NSLog(@"33333333Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
//    });
//    
//    dispatch_async(systemConcurrent, ^{
//        NSLog(@"44444444Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
//    });
    
    //2.         
    dispatch_queue_t myConcurrentQueue = dispatch_queue_create("com.lanou.BJS", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(myConcurrentQueue, ^{
        NSLog(@"11111111Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
    });
    
    dispatch_async(myConcurrentQueue, ^{
        NSLog(@"22222222Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
    });
    
    dispatch_async(myConcurrentQueue, ^{
        NSLog(@"33333333Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
    });
    
    dispatch_async(myConcurrentQueue, ^{
        NSLog(@"44444444Thresd:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
    });
    
    
}

//       (after)
- (IBAction)delay:(id)sender {
    
    double sec = 3;
    //        (                    ,          ,             )
    dispatch_time_t myTime = dispatch_time(DISPATCH_TIME_NOW, sec * NSEC_PER_SEC);
    //          ,         ,block          
    dispatch_after(myTime, dispatch_get_main_queue(), ^{
        NSLog(@"3   ,    ");
    });
    
}

//(group)
//                      
- (IBAction)groupBt:(id)sender {
    
    //     group
    dispatch_group_t myGroup = dispatch_group_create();
    
    //          group  
    dispatch_group_async(myGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSLog(@"11111  ");
    });
    
    dispatch_group_async(myGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSLog(@"22222  ");
    });
    
    dispatch_group_async(myGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSLog(@"33333  ");
    });
    
    dispatch_group_async(myGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSLog(@"44444  ");
    });
    
    //         group.    group           ,      block
    dispatch_group_notify(myGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSLog(@"group            ");
    });
    
    
}
// (barrier)
//                    ,    ,                    
//       barrier    2  , 3  
- (IBAction)barrier:(id)sender {
    //                  ,                   ,        
    
    //      
    dispatch_queue_t myQueue = dispatch_queue_create("com.lanou.BJS", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(myQueue, ^{
    NSLog(@"  1");
    });

    dispatch_async(myQueue, ^{
        NSLog(@"  2");
    });
    
    dispatch_barrier_async(myQueue, ^{
        NSLog(@"  2 3       ");
    });
    
    dispatch_async(myQueue, ^{
        NSLog(@"  3");
    });
    
    dispatch_async(myQueue, ^{
        NSLog(@"  4");
    });

}
//        (apply)
- (IBAction)apply:(id)sender {
    //             ,block         block        size_t          
    
    dispatch_apply(10, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(size_t size) {
        NSLog(@"%ld",size);
        NSLog(@"   ");
    });
    
}
//           (Once)
- (IBAction)once:(id)sender {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"         ,      ");
    });
    
}

//   once      
+(ViewController *)defaultViewController
{
    static ViewController *currentVC;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        currentVC = [[ViewController alloc]init];
    });
    return currentVC;
}
- (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.
}

@end