objective-c创建线程的3种方式

本文介绍了使用Objective-C创建线程的三种不同方法:通过NSThread实例化、从当前线程分离新线程以及在后台执行指定选择器。每种方式都有其适用场景和特点。

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

/*
 * 创建线程的方式1
 */
- (void)createThread1
{
    // 创建线程
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download) object:nil];
    
//    // 起线程名字
//    thread.name = @"下载线程";
    
    // 启动线程(调用self的download方法)
    [thread start];
}
/*
 * 创建线程的方式2
 */
- (void)createThread2
{
    // 从当前线程中分离出一条线程
    [NSThread detachNewThreadSelector:@selector(download:) toTarget:self withObject:@"http://awdawdaw"];
    // withObject:传参
}
/*
 * 创建线程的方式3:隐式创建
 */
- (void)createThread3
{
    // 这两个不会创建线程,在当前线程进行
//    [self performSelector:@selector(download:) withObject:@"http://afaf"];
//    [self download:@"http://awdaf"];
    [self performSelectorInBackground:@selector(download:) withObject:@"http://awdaf"];
    
//    self performSelector:@selector(download:) onThread:[on mainThread] withObject:<#(id)#> waitUntilDone:<#(BOOL)#> modes:<#(NSArray *)#>
}





                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值