• 多线程 NSThread GCD


    ios多线程实现种类

    NSThread

    NSOperationQueue

    NSObject
    GCD 

    ***************

    1.NSThread

       //线程

       第一种

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

    //    

    //    //给线程起名字

        thread1.name=@"thread1";

    //    //启动线程

        [thread1 start];

        //结束线程

        [thread1 cancel];

       第二种(不需要手动开启的)

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

     

     

    2. NSOperation的两个子类

        

        //.

        NSInvocationOperation *inop=[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(invocation) object:nil];

        

     //   [inop start];

        //.

        NSBlockOperation *blop=[NSBlockOperation blockOperationWithBlock:^{

            

            NSLog(@"我是block");

        }];

        

        //创建队列

        NSOperationQueue *queue=[[NSOperationQueue alloc] init];

        

        //设置最大并行数量

        queue.maxConcurrentOperationCount=2;

        

        //添加事件

        [queue addOperation:inop];

       

        [queue addOperation:blop];

        

     

     

    3.NSObject

     [self performSelectorInBackground:@selector(sum) withObject:nil];

    4.GCD

        //GCD (先进先出 FIFO)

        //串行:前一个任务完成,后一个任务才能执行

        //并行:任务在派发时有序的,但是不应等第一个任务执行完成才开始.

        //GCD队列分3中:主队列,全局队列,自定义队列

        //1.使用主队列实现任务派发(串行),在主线程中

        dispatch_queue_t mainQueue=dispatch_get_main_queue();

        //1.1添加任务

        dispatch_async(mainQueue, ^{

            NSLog(@"第一个任务:当前线程是%@",[NSThread currentThread]);

        });

     //串行

        //2.自定义队列

        /*

        dispatch_queue_t myQueue=dispatch_queue_create("com.lanlan.myqueue", DISPATCH_QUEUE_SERIAL);

        //2.1添加任务

        

        dispatch_async(myQueue, ^{

            

            NSLog(@"第一个任务:当前线程:%@",[NSThread currentThread]);

        });

    //并行

        //3.自定义队列

      /*

        dispatch_queue_t myQueue2=dispatch_queue_create("com.lanlan.myqueue", DISPATCH_QUEUE_CONCURRENT);

        

        //3.1添加任务

        dispatch_async(myQueue2, ^{

            NSLog(@"第一个任务:当前线程:%@",[NSThread currentThread]);

        });

        

     //4.全局队列

       /*

        dispatch_queue_t globelQueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        

        

        //4.1添加任务

        

        dispatch_async(globelQueue, ^{

            NSLog(@"第一个任务:当前线程:%@",[NSThread currentThread]);

        });

        

    5.只保证执行一次

       //保证只执行一次

        

        static dispatch_once_t onceToken;

        dispatch_once(&onceToken, ^{

            NSLog(@"这里的代码只执行一次");

        });

        

    6.延迟5秒后执行

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            NSLog(@"等我5秒...");

        });

    7.//重复执行

        dispatch_apply(5, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t t) {

            NSLog(@"今天天气不错");

        });

     //子线程

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

     //回到主线程

        [self performSelectorOnMainThread:@selector(mymainTherad) withObject:self waitUntilDone:NO];

    -(void)mymainTherad

    {

    //打印线程

       NSLog(@"我在主线程里%@",[NSThread currentThread]);

    }

     

  • 相关阅读:
    vue实例讲解之vuex的使用
    实例讲解webpack的基本使用第一篇
    如何写一个jquery插件
    《改变你一生的108个心理学法则》读书笔记
    用css绘制各种图形
    关于js浮点数计算精度不准确问题的解决办法
    非常有用的css使用总结
    网页meta标签总结
    Object.defineProperty()方法的用法详解
    js中set和get的用法
  • 原文地址:https://www.cnblogs.com/haiying/p/4122183.html
Copyright © 2020-2023  润新知