zoukankan      html  css  js  c++  java
  • 接上 主线程和分线程的另一种方法GCD

    //

    //  ViewController.m

    //  GCD

    //

    //  Created by mac on 15-9-28.

    //  Copyright (c) 2015年 zy. All rights reserved.

    //

     

    #import "ViewController.h"

     

    @interface ViewController ()

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad

    {

        [super viewDidLoad];

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

    //    Gcd grand central dispatch

    //    dispath_queue_t  类似于操作队列

    //    可以在使用GCD创建串行队列,创建并行队列

    #pragma mark-----串行队列------

    //    参数一:队列标示符

    //    参数二:表示队列类型,串行还是并行

    //    DISPATCH_QUEUE_SERIAL 串行队列

    //    DISPATCH_QUEUE_CONCURRENT 并行队列

        

      /*  dispatch_queue_t queue=dispatch_queue_create("queue1", DISPATCH_QUEUE_SERIAL);

        //    可以在队列中添加同步内容和异步内容

    //    sync 同步      async异步

        dispatch_sync(queue, ^{

           

            NSLog(@"任务一开始任务");

            [NSThread sleepForTimeInterval:2];

            NSLog(@"任务一 ------%d",[NSThread isMainThread]);

        });

        dispatch_sync(queue, ^{

            NSLog(@"任务二开始任务");

            [NSThread sleepForTimeInterval:2];

            NSLog(@"任务二 ------%d",[NSThread isMainThread]);

        });

        

        dispatch_async(queue, ^{

            NSLog(@"任务三开始任务");

            [NSThread sleepForTimeInterval:2];

            NSLog(@"任务三------%d",[NSThread isMainThread]);

        });

        

        dispatch_async(queue, ^{

            NSLog(@"任务四开始任务");

            [NSThread sleepForTimeInterval:2];

            NSLog(@"任务四------%d",[NSThread isMainThread]);

        });

    //    串行队列是可以暂停的

        dispatch_suspend(queue);

    //    队列继续

        dispatch_resume(queue);

    //    使用串行队列,任务需要等待的,必须等到上一个任务结束之后,才能执行下一个任务

    //    同步任务是在主线程中执行的,异步任务是在分线程中执行的*/

    #pragma mark - --并行队列-----

    //    dispatch_get_global_queue  可以获取到系统提供的并行队列

    //    参数一:队列优先级 HIGH > DEFAULT > LOW >BACKGROUND

    //    参数二:系统预留字段

      dispatch_queue_t queue=  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    //    同步任务

        dispatch_sync(queue, ^{

            

            NSLog(@"任务一开始执行");

            [NSThread sleepForTimeInterval:2];

            NSLog(@"任务一完成%d",[NSThread isMainThread]);

        });

        dispatch_sync(queue, ^{

            

            NSLog(@"任务二开始执行");

            [NSThread sleepForTimeInterval:2];

            NSLog(@"任务二完成%d",[NSThread isMainThread]);

        });

        dispatch_async(queue, ^{

           

            NSLog(@"任务三开始执行");

            [NSThread sleepForTimeInterval:2];

            NSLog(@"任务三完成%d",[NSThread isMainThread]);

        });

        dispatch_async(queue, ^{

            

            NSLog(@"任务四开始执行");

            [NSThread sleepForTimeInterval:2];

            NSLog(@"任务四完成%d",[NSThread isMainThread]);

        });

    //    并行队列中, 如果任务是同步任务,任务也是需要等待的,此时任务是在主线程中执行的

    //    如果任务是异步任务,任务不需等待,可以并发执行,此时任务是在分线程中执行的

    //    并行队列是不可以暂停的

        

        

        

    #pragma mark----主线程队列------

        dispatch_queue_t mainQueue = dispatch_get_main_queue();

        dispatch_async(mainQueue, ^{

            

            NSLog(@"任务五开始执行");

            [NSThread sleepForTimeInterval:2];

            NSLog(@"任务五完成%d",[NSThread isMainThread]);

        });

    //    一般向主线程中队列中添加任务时,添加异步任务

    //    主线程队列是不可以暂停的

    //    主线程刷新UI

    }

     

    - (void)didReceiveMemoryWarning

    {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    @end

  • 相关阅读:
    前沿技术解密——VirtualDOM
    Ques核心思想——CSS Namespace
    Unix Pipes to Javascript Pipes
    Road to the future——伪MVVM库Q.js
    聊聊CSS postproccessors
    【译】十款性能最佳的压缩算法
    Kafka Streams开发入门(9)
    Kafka Streams开发入门(8)
    【译】Kafka Producer Sticky Partitioner
    【译】99th Percentile Latency at Scale with Apache Kafka
  • 原文地址:https://www.cnblogs.com/wangzhen-Me/p/4844911.html
Copyright © 2011-2022 走看看