zoukankan      html  css  js  c++  java
  • 多线程 GCD

    //  DYFViewController.m
    //  623-07-GCD
    //
    //  Created by dyf on 14-6-23.
    //  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
    //
     
    #import "DYFViewController.h"
     
    @interface DYFViewController ()
    @property (weak, nonatomic) IBOutlet UIImageView *iconView;
     
    @end
     
    @implementation DYFViewController
     
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
         
         
         
         
        /*
        // 异步:具备开启新线程的能力
        dispatch_async(<#dispatch_queue_t queue#>, ^{
            <#code#>
        });
        // 同步:不具备开启新线程的能力
        dispatch_sync(<#dispatch_queue_t queue#>, ^{
            <#code#>
        })
         */
    }
     
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self testBackToMain];
    }
     
    - (void)testBackToMain {
        // 获取全局并发队列
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        // 异步队列
        dispatch_async(queue, ^{
            NSLog(@"-----%@", [NSThread currentThread]);
            // 下载图片
            NSString *path = @"http://image.cache.xiu8.com/live/125/125/997729.jpg";
            NSURL *url = [NSURL URLWithString:path];
            NSData *data = [NSData dataWithContentsOfURL:url];
             
            UIImage *image = [UIImage imageWithData:data];
            // 回到主线程显示图片
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"-----------%@", [NSThread currentThread]);
                self.iconView.image = image;
            });
        });
    }
     
    - (void)testOnce {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            NSLog(@"once");
        });
    }
     
    - (void)testDelay {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"222");
        });
    }
     
    /**
     *  使用dispatch_async异步函数,在主线程中网主队列中添加任务
     */
    - (void)testAsyncMainQueue {
        // 1.获得主队列
        dispatch_queue_t queue = dispatch_get_main_queue();
        // 2.添加任务到队列中,执行任务
        dispatch_async(queue, ^{
            NSLog(@"---------1-----%@", [NSThread currentThread]);
        });
     
        // 总结:不开新线程
         
    }
     
    /**
     *  使用dispatch_sync同步函数,在主线程中网主队列中添加任务,死:任务无法往下执行
     */
    - (void)testSyncMainQueue {
        // 1.获得主队列
        dispatch_queue_t queue = dispatch_get_main_queue();
        // 2.添加任务到队列中,执行任务
        dispatch_sync(queue, ^{
            NSLog(@"---------1-----%@", [NSThread currentThread]);
        });
         
     
        // 总结:不开新线程,所有任务在主线程中串行执行
    }
     
    // 凡是函数名中带有create、copy、new、retain等字眼,都需要在不需要使用这个数据的时候进行release
    // GCD的数据类型在ARC环境下不需要再做release
    // CF(Core Foundation)的数据类型在ARC环境下仍然要做release
     
    - (void)testCF {
        CFArrayRef array = CFArrayCreate(NULL, NULL, 11, NULL);
        CFRelease(array);
    }
     
    /**
     *  用dispatch_sync同步函数往串行队列中添加任务
     */
    - (void)testSyncSerialQueue {
        // 1.创建串行队列
        dispatch_queue_t queue = dispatch_queue_create("cn.dongyue.queue", NULL);
         
        // 2.添加任务到队列中,执行任务
        dispatch_sync(queue, ^{
            NSLog(@"---------1-----%@", [NSThread currentThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"---------2-----%@", [NSThread currentThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"---------3-----%@", [NSThread currentThread]);
        });
         
        // 3.释放(MRC)
        //dispatch_release(queue);
         
         
        // 总结:不会开新的线程
    }
     
    /**
     *  用dispatch_sync同步函数往并发队列中添加任务
     */
    - (void)testSyncGlobalQueue {
        // 1.获得全局的并发队列
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
         
        // 2.添加任务到队列中,执行任务
        dispatch_sync(queue, ^{
            NSLog(@"---------1-----%@", [NSThread currentThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"---------2-----%@", [NSThread currentThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"---------3-----%@", [NSThread currentThread]);
        });
     
        // 总结:不会开启新的线程,并发队列失去了并发功能
    }
    /**
     *  用dispatch_async同步函数往并发队列中添加任务
     */
    - (void)testAsyncSerialQueue {
        // 1.创建串行队列
        dispatch_queue_t queue = dispatch_queue_create("cn.dongyue.queue", NULL);
         
        // 2.添加任务到队列中,执行任务
        dispatch_async(queue, ^{
            NSLog(@"---------1-----%@", [NSThread currentThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"---------2-----%@", [NSThread currentThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"---------3-----%@", [NSThread currentThread]);
        });
         
        // 总结:只开1个新的线程,不会开启新的线程
    }
    /**
     *  用dispatch_async同步函数往并发队列中添加任务
     */
    - (void)testAsyncGlobalQueue {
        // 1.获得全局的并发队列
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
         
        // 2.添加任务到队列中,执行任务
        dispatch_async(queue, ^{
            NSLog(@"---------1-----%@", [NSThread currentThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"---------2-----%@", [NSThread currentThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"---------3-----%@", [NSThread currentThread]);
        });
        // 总结:同时开启了3个线程
    }
     

    @end

     
     
  • 相关阅读:
    Vue框架之基础知识
    Vue框架之初识
    Django组件之modelformset
    Django之小结
    Django组件之modelform
    Django之form表单详解
    jquery基础知识2
    jQuery基础知识1
    js基础知识4
    js基础知识3
  • 原文地址:https://www.cnblogs.com/Cheetah-yang/p/4664151.html
Copyright © 2011-2022 走看看