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

     

            // NSThread 创建线程

            // 1. initWithTarget:selector:object: 创建,需要调用start方法进行启动

            NSThread *thread1 = [[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];

            [thread1 start];

            // 2.

            [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];

     

     

        

     

        // gcd 创建线程

        // 1. 创建串行队列;将任务提交给队列,必须等第一个任务完成后,才执行第二个任务

        dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);

        dispatch_async(serialQueue, ^(void)

                       {

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

                               NSLog(@"serialQueue .... %d", i);

                           }

                       });

        dispatch_async(serialQueue, ^(void)

                       {

                           for(int i=10; i<20; i++)

                           {

                               NSLog(@"serialQueue ... %d", i);

                           }

                       });

        // 2. 创建并发队列;将任务提交给队列,第一个任务和第二个任务,并行执行

        dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);

        dispatch_async(concurrentQueue, ^(void)

                       {

                           for(int i=0; i<10; i++)

                           {

                               NSLog(@"concurrentQueue...i=%d", i);

                           }

                       });

        dispatch_async(concurrentQueue, ^(void)

                       {

                           for(int i=10;i<20;i++)

                           {

                               NSLog(@"concurrentqueue...i=%d",i);

                           }

                       });

     

      

        // 全局并发队列

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void)

                       {

                           for(int i=0; i<10; i++)

                           {

                               NSLog(@"global_queue...i=%d", i);

                           }

                       });

        // 主线程

        dispatch_async(dispatch_get_main_queue(), ^(void)

                       {

                           NSLog(@"main thread...");

                       });

  • 相关阅读:
    软件设计7个原则
    vue.js 样式绑定 font-size 问题
    实例理解scala 隐式转换(隐式值,隐式方法,隐式类)
    著名端口整理(常用服务的默认端总结)
    .NET Core Web API 实现大文件分片上传
    ngnix反向代理tomcat,ssl证书配置及自定义错误页面
    微信登录闪退
    gradle如何配置阿里云的中央仓库
    HashMap底层实现和原理
    关于Java中String类的hashCode方法
  • 原文地址:https://www.cnblogs.com/xiangjune/p/5194470.html
Copyright © 2011-2022 走看看