• GCD 的使用


    //
    //  ZYGCDViewController.h
    //  Thread
    //
    //  Created by wanglixing on 14/11/4.
    //  Copyright © 2014年 zzz. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface ZYGCDViewController : UIViewController
    
    @end
    //
    //  ZYGCDViewController.m
    //  Thread
    //
    //  Created by wanglixing on 14/11/4.
    //  Copyright © 2014年 zzz. All rights reserved.
    //
    
    #import "ZYGCDViewController.h"
    
    @interface ZYGCDViewController ()
    
    @end
    
    @implementation ZYGCDViewController
    
    - (void)viewDidLoad {
        self.view.backgroundColor = [UIColor whiteColor];
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        //GCD 在实现多线程的时候有两个优点:
        //1.可以充分利用多核处理器的性能。
        //2.使用起来简单方便,不需要管理线程的生命周期,可以使我们更关注于需要在线程中执行的任务。
        
        //GCD 使用队列(queue)管理线程。队列一般分两种:
        //1.运行在主线程的队列,mainQueue。一次只能执行一个任务,遵循先进先出的原则(FIFO),串行队列。
        //获得 mainQueue 队列。
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        
        //2.运行在分线程的队列,四个优先级不同的 globalQueue。并行队列,同时可以执行多个任务。globalQueue 管理了一组的分线程,实际运行的时候使用哪一个分线程,我们是不能控制的。
        
    //#define DISPATCH_QUEUE_PRIORITY_HIGH 2
    //#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
    //#define DISPATCH_QUEUE_PRIORITY_LOW (-2)
    //#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN
        
        //第一个参数是优先级,第二个参数无意义。
        dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        
        //只需要把任务提交到队列中,任务就会在对应的线程中执行了。
        //同步,阻塞线程,等待任务执行完毕才继续执行后续代码。
        //异步,同时执行。
        //同步提交,如果在主线程使用,会导致线程死锁。
    //    dispatch_sync(<#dispatch_queue_t queue#>, <#^(void)block#>);
        
    //    NSLog(@"=========");
    //
    //     dispatch_sync(mainQueue, ^{
    //         NSLog(@"----------");
    //     });
    //    
    //    NSLog(@"+++++++++++");
        
        //异步提交,一般只使用这种方法。
        dispatch_async(mainQueue, ^{
            //block 的内容在主线程执行。
            
            if ([NSThread isMainThread]) {
                NSLog(@"主线程");
            }else {
                NSLog(@"分线程");
            }
        });
        
        
        dispatch_async(globalQueue, ^{
            //block 的内容在分线程执行。
            
            if ([NSThread isMainThread]) {
                NSLog(@"=====主线程");
            }else {
                NSLog(@"=====分线程");
            }
        });
        
        //串行队列,先进先出,依次执行。
    //    dispatch_async(mainQueue, ^{
    //        NSLog(@"11111111111");
    //        NSLog(@"11111111111");
    //        NSLog(@"11111111111");
    //        NSLog(@"11111111111");
    //        NSLog(@"11111111111");
    //    });
    //    
    //    dispatch_async(mainQueue, ^{
    //        NSLog(@"22222222222");
    //        NSLog(@"22222222222");
    //        NSLog(@"22222222222");
    //        NSLog(@"22222222222");
    //        NSLog(@"22222222222");
    //    });
    //    
    //    dispatch_async(mainQueue, ^{
    //        NSLog(@"33333333333");
    //        NSLog(@"33333333333");
    //        NSLog(@"33333333333");
    //        NSLog(@"33333333333");
    //        NSLog(@"33333333333");
    //    });
    //    
    //    dispatch_async(mainQueue, ^{
    //        NSLog(@"44444444444");
    //        NSLog(@"44444444444");
    //        NSLog(@"44444444444");
    //        NSLog(@"44444444444");
    //        NSLog(@"44444444444");
    //    });
        
        
        //并行队列,多个任务同时执行。
    //    dispatch_async(globalQueue, ^{
    //        NSLog(@"11111111111");
    //        NSLog(@"11111111111");
    //        NSLog(@"11111111111");
    //        NSLog(@"11111111111");
    //        NSLog(@"11111111111");
    //    });
    //    
    //    dispatch_async(globalQueue, ^{
    //        NSLog(@"22222222222");
    //        NSLog(@"22222222222");
    //        NSLog(@"22222222222");
    //        NSLog(@"22222222222");
    //        NSLog(@"22222222222");
    //    });
    //    
    //    dispatch_async(globalQueue, ^{
    //        NSLog(@"33333333333");
    //        NSLog(@"33333333333");
    //        NSLog(@"33333333333");
    //        NSLog(@"33333333333");
    //        NSLog(@"33333333333");
    //    });
    //    
    //    dispatch_async(globalQueue, ^{
    //        NSLog(@"44444444444");
    //        NSLog(@"44444444444");
    //        NSLog(@"44444444444");
    //        NSLog(@"44444444444");
    //        NSLog(@"44444444444");
    //    });
        
        
        //可以自定义队列,但是一般用不上,系统提供给我们的两个已经够用了。
    //    dispatch_queue_create("com.www.zhiyou", nil);
        
        //系统的某些方法也会用到 mainQueue 和 globalQueue。
        
        //通常这样使用 GCD。
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //去分线程执行代码。
            dispatch_async(dispatch_get_main_queue(), ^{
               //切回主线程,刷新 UI。
            });
        });
        
        
        //下载图片。
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
           //下载图片。dataWithContentsOfURL 是同步请求。
            NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://xxx/1.jpg"]];
            
            UIImage* image = [UIImage imageWithData:data];
            
            //切换回主线程。
            dispatch_async(dispatch_get_main_queue(), ^{
                UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
                
                [self.view addSubview:imageView];
                
                [imageView release];
            });
        });
        
        
        
        //等待某些任务执行完毕后,再去执行另外一个任务。
        dispatch_group_t group = dispatch_group_create();
        
        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //        下载图片1。
            NSLog(@"11111111111");
            NSLog(@"11111111111");
            NSLog(@"11111111111");
            NSLog(@"11111111111");
            NSLog(@"11111111111");
        });
        
        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //下载图片2。
            NSLog(@"22222222222");
            NSLog(@"22222222222");
            NSLog(@"22222222222");
            NSLog(@"22222222222");
            NSLog(@"22222222222");
        });
        
        //等待 group 中的全部任务执行完毕,才会执行这里的任务。
        dispatch_group_notify(group, dispatch_get_main_queue(), ^{
            NSLog(@"33333333333");
            NSLog(@"33333333333");
            NSLog(@"33333333333");
            NSLog(@"33333333333");
            NSLog(@"33333333333");
        });
    }
    
    @end
  • 相关阅读:
    numpy 基础 —— np.linalg
    图像旋转后显示不完全
    opencv ---getRotationMatrix2D函数
    PS1--cannot be loaded because the execution of scripts is disabled on this system
    打开jnlp Faild to validate certificate, the application will not be executed.
    BATCH(BAT批处理命令语法)
    oracle vm virtualbox 如何让虚拟机可以上网
    merge 实现
    Windows batch,echo到文件不成功,只打印出ECHO is on.
    python2.7.6 , setuptools pip install, 报错:UnicodeDecodeError:'ascii' codec can't decode byte
  • 原文地址:https://www.cnblogs.com/ningmengcao-ios/p/5771240.html
Copyright © 2020-2023  润新知