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...");

                       });

  • 相关阅读:
    sql语句最后一行显示统计。
    Win10访问不到XP共享的解决:
    git-github-TortoiseGit综合使用教程(二)快速入门
    git-github-TortoiseGit综合使用教程(一)简介
    RHEL7 -- 修改主机名
    安装完 MySQL 后必须调整的 10 项配置(转)
    my.cnf
    mysql查看系统参数
    MySQL性能的五大配置参数(内存参数)
    (转)Linux用户登录记录日志和相关查看命令汇总
  • 原文地址:https://www.cnblogs.com/xiangjune/p/5194470.html
Copyright © 2011-2022 走看看