GCD 的使用

来源:互联网 发布:mysql 查询加分页 编辑:程序博客网 时间:2024/06/14 15:21

//  ViewController.m

//  UsedGCD

//

//  Created by jinxin on 16/8/1.

//  Copyright © 2016年 zhaixingzhi. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    

    [self mainSync];;

}

/**

 *  主队列 + 异步任务 : 没有开启新线程,任务还是逐个完成的

 */

-(void)mainAsync

{

    dispatch_queue_t queue =dispatch_get_main_queue();

    dispatch_async(queue, ^{

        for (int

             i = 0; i< 6; i++) {

            NSLog(@"1----%@",[NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i< 6; i++) {

            NSLog(@"2----%@",[NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i< 6; i++) {

            NSLog(@"3----%@",[NSThreadcurrentThread]);

        }

    });

    


}

/**

 *  主队列 + 同步任务:会造成死锁的进程 【切记】:不能字啊主队列中添加同步任务

 */

-(void)mainSync

{

    NSLog(@"---1");

    dispatch_queue_t queue =dispatch_get_main_queue();

    dispatch_sync(queue, ^{

        NSLog(@"不会打印");

    });

    NSLog(@"---2");

    

    

}


/**

 *  串行队列 +异步任务,开启了新的线程,任务是逐个完成的

 

 */

-(void)serialAsync

{

    dispatch_queue_t queue =dispatch_queue_create("queue",NULL);

    

    dispatch_async(queue, ^{

        for (int

             i = 0; i< 6; i++) {

            NSLog(@"1----%@",[NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i< 6; i++) {

            NSLog(@"2----%@",[NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i< 6; i++) {

            NSLog(@"3----%@",[NSThreadcurrentThread]);

        }

    });


}

/**

 *  串行队列 + 同步任务:没有开启新的线程,任务是逐个完成

 */

-(void)serialSync

{

//创建串行队列

    dispatch_queue_t queue =dispatch_queue_create("queue",DISPATCH_QUEUE_SERIAL);//这里可以用DISPATCH_QUEUE_SERIAL或者NULL都是串行队列

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-1--%@",[NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-2--%@",[NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-3--%@",[NSThreadcurrentThread]);

        }

    });


}

/**

 *  全局队列 + 异步任务 : 开启新的线程,任务是并发的

 */

-(void)globalAsync

{

    dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

    

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-1--%@",[NSThreadcurrentThread]);

        }

   

    });


    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-2--%@",[NSThreadcurrentThread]);

        }

        

    });

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-3--%@",[NSThreadcurrentThread]);

        }

        

    });

}

/**

 *  全局队列 + 同步任务 : 没有开启新线程 ,任务是逐个完成的

 */

-(void)globalSync

{

    //获得全局队列

    dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

    

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-1--%@",[NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-2--%@",[NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-3--%@",[NSThreadcurrentThread]);

        }

    });


}


/**

 *  并发队列 + 异步任务 : 开启了新线程,任务是并发的

 *

 */

-(void)concurrentAsunc

{

    //创建并发队列

    dispatch_queue_t queue =dispatch_queue_create("myQueue",DISPATCH_QUEUE_CONCURRENT);

    //异步任务

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-1--%@",[NSThreadcurrentThread]);

        }

  

    });

    //异步任务

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-2--%@",[NSThreadcurrentThread]);

        }

        

    });

    //异步任务

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-3--%@",[NSThreadcurrentThread]);

        }

        

    });


}


/**

 *  并发队列 + 同步任务 : 没有开启新的线程,任务逐个进行

 */

-(void)concurrentSync

{

    //创建并行队列

    dispatch_queue_t queue =dispatch_queue_create("myQueue",DISPATCH_QUEUE_CONCURRENT);

    //在对垒中创建任务

    //同步任务

    

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-1--%@",[NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-2--%@",[NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"-3--%@",[NSThreadcurrentThread]);

        }

    });


}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

原创粉丝点击