GCD 多线程的学习(基本了解…

#import "ViewController.h"

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

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

    

    //正确认识当前线程

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

    dispatch_async(queue, ^{

        NSLog(@"并行队列异步执行开始%@", [NSThread currentThread]);

        //在同步执行任务

        //当前线程为子线程

        dispatch_sync(queue, ^{

            NSLog(@"XXXXX:%@", [NSThread currentThread]);

        });

    });

    

}


//串行队列同步执行

- (IBAction)serialQueueSync:(id)sender {

   

    //1.创建串行队列(给定名字+指定队列类型)

    NSLog(@"开始执行啦:%@", [NSThread currentThread]);

    dispatch_queue_t queue = dispatch_queue_create("FirstSerialQueue", DISPATCH_QUEUE_SERIAL);

    //2.添加两个任务到串行队列中(block)

    //3.同步执行两个任务

    dispatch_sync(queue, ^{

        //添加第一个任务(耗时操作)

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

            [NSThread sleepForTimeInterval:1];

            NSLog(@"++++++++++%@",[NSThread currentThread]);

        }

    });

    NSLog(@"打印+结束");

 

    dispatch_sync(queue, ^{

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

            [NSThread sleepForTimeInterval:1];

            NSLog(@"----------%@", [NSThread currentThread]);

        }

    });

    NSLog(@"打印结束");

}

//串行队列异步执行

- (IBAction)serialQueueAsync:(id)sender {

   

    dispatch_queue_t queue = dispatch_queue_create("SecondSerialQueue", DISPATCH_QUEUE_SERIAL);

    dispatch_async(queue, ^{

        //添加到队列中的任务

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

            [NSThread sleepForTimeInterval:1];

            NSLog(@"+++++++++%@", [NSThread currentThread]);

        }

    });

    NSLog(@"打印+完毕");

    dispatch_async(queue, ^{

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

            [NSThread sleepForTimeInterval:1];

            NSLog(@"-------%@",[NSThread currentThread]);

        }

    });

    NSLog(@"打印-完毕");

}

//并行队列同步执行

- (IBAction)concurrentQueueSync:(id)sender {

   

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

    dispatch_sync(queue, ^{

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

            [NSThread sleepForTimeInterval:1];

            NSLog(@"++++++++%@",[NSThread currentThread]);

        }

    });

    NSLog(@"打印+完毕");

    

    dispatch_sync(queue, ^{

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

            [NSThread sleepForTimeInterval:1];

            NSLog(@"--------%@",[NSThread currentThread]);

        }

    });

    NSLog(@"打印-完毕");

}

//并行队列异步执行

- (IBAction)concurrentQueueAsync:(id)sender {

   

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

    dispatch_async(queue, ^{

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

            [NSThread sleepForTimeInterval:1];

            NSLog(@"++++++++%@", [NSThread currentThread]);

        }

    });

    NSLog(@"打印+完毕");

    

    dispatch_async(queue, ^{

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

            [NSThread sleepForTimeInterval:1];

            NSLog(@"--------%@", [NSThread currentThread]);

        }

    });

    NSLog(@"打印-完毕");

}






//全局队列异步执行(掌握)

- (IBAction)globalQueueAsync:(id)sender {

    //结论和并行队列异步执行一样

    //1.获取全局队列(只有这一步不一样)

   

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    //2.添加任务

    dispatch_async(queue, ^{

        NSLog(@"++++++%@", [NSThread currentThread]);

    });

    //3.异步执行任务

    NSLog(@"打印+完毕");

}

//主队列异步执行

- (IBAction)mainQueueAsync:(id)sender {

    //1.获取主队列

    dispatch_queue_t queue = dispatch_get_main_queue();

    //2.添加到主队列

    dispatch_async(queue, ^{

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

            [NSThread sleepForTimeInterval:1];

            NSLog(@"+++++++++%@", [NSThread currentThread]);

        }

    });

    NSLog(@"打印+完毕");

    dispatch_async(queue, ^{

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

            [NSThread sleepForTimeInterval:1];

            NSLog(@"---------%@", [NSThread currentThread]);

        }

    });

    NSLog(@"打印-完毕");

}

//主队列同步执行

- (IBAction)mainQueueSync:(id)sender {

//    dispatch_queue_t queue = dispatch_get_main_queue();

    NSLog(@"任务一");

    dispatch_sync(dispatch_get_main_queue(), ^{

        NSLog(@"任务二");

    });

    NSLog(@"任务三");

    

}





- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


 

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值