ios中的多线程的用法总结

本文详细介绍了iOS中多线程的基本概念、创建方法及注意事项,包括进程与线程的区别、多线程的优势与限制、如何正确使用多线程提高程序性能等。同时,文章还涉及了NSRunLoop的作用与使用、定时器在多线程中的应用,以及GCD的使用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ios中的多线程的用法总结

1、进程的基本概念

(1)每一个进程都是一个应用程序,都有独立的内存空间,一般来说一个应用程序存在一个进程,但也有多个进程的情况

(2)同一个进程的线程共享内存中的内存和资源

2、多线程的基本概念

(1)每一个程序都有一个主线程,程序启动时创建(调用main来启动)。

(2)多线程技术表示,一个应用程序有多个线程,使用多线程能提供CPU的利用率,防止主线程被堵塞。

(3)任何有可能堵塞主线程的任务不要在主线程执行(如:访问网络)。

(4)主线程的生命周期和应用程序绑定着,程序退出(结束)时,主线程也结束。

3、多线程的创建

                    /* 方法一*/   

    //第一种开启新的线程调用 threadFunction

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

    [thread start];

                      /*方法二:*/

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

                       /*方法三:*/

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

  /*方法四:*/

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

    [operationQueue addOperationWithBlock:^(void){

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

            NSLog(@"多线程的运行");

        }

        

    }];

                    /*方法五:*/

    //创建一个线程队列

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

    //设置线程执行的并发数

    operationQueue.maxConcurrentOperationCount = 1;

    //创建一个线程操作对象

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

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

    //设置线程的优先级

    operation2.queuePriority = NSOperationQueuePriorityHigh;

    //将线程添加到线程队列中去

    [operationQueue addOperation:operation1];

    [operationQueue addOperation:operation2];

        }

    

    /*第六种方法*/

GCD

GCD是Grand Central Dispatch的缩写,是一系列的BSD层面的接口,在Mac 10.6 和iOS4.0以后才引入的,且现在NSOperation和NSOperationQueue的多线程的实现就是基于GCD的。目前这个特性也被移植到 FreeBSD上了,可以查看libdispatch这个开源项目。


    //创建一个队列

    dispatch_queue_t queue = dispatch_queue_create("test", NULL);

//创建异步进程

    dispatch_async(queue, ^{

dispatch_sync(dispatch_get_main_queue(),^{

});

    });


  4、NSRunLoop的用法  

(1)Runloop是与线程有关的基础框架的一部分,是用来规划事件处理的,当有任务的时候Runloop会让线程处理任务,当没有任务的时候Runloop会让线程处于休眠状态。
(2)Runloop的管理不完全是自动的,我们必须在合适的时候开启Runloop和处理到达的事件,Cocoa和Core Foundation都提供了Runloop对象来配置和管理线程的Runloop。我们的应用程序不需要显示的创建这些对象,包括应用主线程在内的每一个线程都有一个相关的Runloop对象。而且只有第二线程是需要显示地运行Runloop,主线程是不需要的,APP把主线程Runloop的配置和运行作为了应用程序启动的一部分。

(3)NSRunLoop可以一直保持一个线程一直为活跃状态,不会马上销毁掉。

(4)操作Runloop的两个接口: 1.NSRunLoop Class Reference 2.CFRunLoop Reference

  5、定时器在多线程的使用

      在多线程中使用定时器必须开启RunLoop,因为只有开启RunLoop保持线程为活动状态,才能保持定时器能不断执行。  

    代码:

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

- (void)mulitiThread{

    

    /*方法一:此方式创建的timer添加至NSRunLoop*/

      [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];

      //开启NSRunLoop来使线程保持存活状态

     [[NSRunLoop currentRunLoop]run];

     */

    

    /*方法二:此方式创建的timer没有添加至NSRunLoop*/

    NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];

    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];

    [[NSRunLoop currentRunLoop]run];

    NSLog(@"线程结束");

}


- (void)timeAction{

    NSLog(@"timeAction");

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值