iOS --- 多线程之NSThread

本文介绍iOS中使用NSThread进行多线程编程的方法,包括创建线程的不同方式、线程属性设置、线程控制及自定义线程实现等。同时探讨了线程锁的应用及Swift环境下使用NSThread的注意事项。

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

NSThread是iOS中底层的线程类,相比GCD和NSOperation更加轻量级,也提供了更为灵活的使用方式,可更直观地控制线程对象,但需要管理线程的生命周期、同步、加锁等问题,会导致一定的性能开销。NSThread的精确度比GCD高,可取消该线程执行,并且可自定义线程。

创建线程

动态方法

NSThread *aThread = [[NSThread alloc] initWithTarget:self selector:@selector(runThread) object:nil];
aThread.threadPriority = 1; // [0.0~1.0],1.0为最高优先级
[aThread start];
[aThread main]; // thread body method
[aThread cancel];

静态方法

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

该方法会立即创建并启动新线程

隐式创建线程

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

其他属性和操作

[NSThread mainThread];
[NSThread currentThread];
[self performSelector:@selector(runThread) withObject:nil]; // 在当前线程执行操作
[self performSelectorOnMainThread:@selector(runThread) withObject:nil waitUntilDone:YES]; // 在主线程执行操作
[self performSelector:@selector(runThread) onThread:thread withObject:nil waitUntilDone:YES]; // 在指定线程上执行操作

performSelector

注意:performSelector相关方法会创建一个timer到当前线程或指定线程的runloop中,若当前线程没有runloop,则该performSelector方法会失效。因主线程有默认的runloop,所以在主线程中执行performSelector,则从不会失效。所以在异步线程中要稍微注意。

不过,performSelector方法在Swift中已弃用,因其在ARC下不安全。原因在于:ARC为了确保参数在方法运行期间的存在,会将输入参数在方法开始时先进行retain,在最后release,而performSelector并没有机会为被调用的selector方法指定参数,所以被调用的selector方法的输入参数可能会指向未知的内存地址,导致crash。

线程锁

NSCondition是锁对象,可用于保护当前访问的资源。

NSCondition *lock = [[NSCondition alloc] init];
[lock lock];
// 线程中执行操作
[lock unLock];

自定义线程

NSThread可以做到自定义线程,如下的Swift代码,定义了CSThread类,继承自NSThread。添加delegate属性,并重写其main()方法,在其中即可指定线程内执行的任务.

import UIKit

protocol CSThreadCompletionDelegate {
    func csThreadCompletion()
}

class CSThread: NSThread {

    var delegate: CSThreadCompletionDelegate!

    override func main() {
        super.main()

        sleep(2)
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            if (self.delegate != nil) {
                self.delegate.csThreadCompletion()
            }
        })
    }
}

使用如下,

let csThread = CSThread()
csThread.delegate = self
csThread.start()

// MARK: - CSThreadCompletionDelegate
func csThreadCompletion() {
    print("csThreadCompletion")
}

Demo

Demo地址: DemoMultiThread

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值