ios 多线程

本文介绍了使用Objective-C进行多线程编程的方法,包括不同线程启动方式的示例及其实现过程。通过具体代码展示了如何创建并启动多个线程,并观察到各线程并发执行的效果。

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

#import "Thread.h"

@implementation Thread

- (void)thread1
{
    //1. 不需要手动设置线程启动,自动启动该线程。
    [NSThread detachNewThreadSelector:@selector(myThreadMethod:) toTarget:self withObject:nil] ;
    //2. 需要设置启动模式,也可以设置线程优先级
    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(myThreadMethod2:) object:nil];
    [thread start];
    
    //3. 间接方式,与第一种相似,不需要手动启动线程
    [self performSelectorInBackground:@selector(myThreadMethod3:) withObject:nil];
    //
    [self performSelector:@selector(myThreadMethod4:) onThread:thread withObject:nil waitUntilDone:NO];
    
    [thread release];
}

- (void)myThreadMethod:(id)arg
{
    for (int i = 0; i < 10; i ++){
        [NSThread sleepForTimeInterval:1];
         NSLog(@"hello wolrd");
    }
}

- (void)myThreadMethod2:(id)arg
{
    for (int i = 0; i < 10; i ++){
        [NSThread sleepForTimeInterval:1];
        NSLog(@"thread 2 ...");
    }
}

- (void)myThreadMethod3:(id)arg
{
    for (int i = 0; i < 10; i ++){
        [NSThread sleepForTimeInterval:1];
        NSLog(@"thread 3 ...");
    }
}

- (void)myThreadMethod4:(id)arg
{
    for (int i = 0; i < 10; i ++){
        [NSThread sleepForTimeInterval:1];
        NSLog(@"thread 4 ...");
    }
}

2013-11-22 17:37:04.124 Project2_OC[3191:1a03] thread 2 ...
2013-11-22 17:37:04.124 Project2_OC[3191:1903] hello wolrd
2013-11-22 17:37:04.124 Project2_OC[3191:2503] thread 3 ...
2013-11-22 17:37:05.128 Project2_OC[3191:1a03] thread 2 ...
2013-11-22 17:37:05.128 Project2_OC[3191:1903] hello wolrd
2013-11-22 17:37:05.128 Project2_OC[3191:2503] thread 3 ...
2013-11-22 17:37:06.131 Project2_OC[3191:1a03] thread 2 ...
2013-11-22 17:37:06.131 Project2_OC[3191:2503] thread 3 ...
2013-11-22 17:37:06.131 Project2_OC[3191:1903] hello wolrd
2013-11-22 17:37:07.133 Project2_OC[3191:2503] thread 3 ...
2013-11-22 17:37:07.133 Project2_OC[3191:1a03] thread 2 ...
2013-11-22 17:37:07.133 Project2_OC[3191:1903] hello wolrd
2013-11-22 17:37:08.135 Project2_OC[3191:2503] thread 3 ...
2013-11-22 17:37:08.135 Project2_OC[3191:1a03] thread 2 ...
2013-11-22 17:37:08.135 Project2_OC[3191:1903] hello wolrd
2013-11-22 17:37:09.138 Project2_OC[3191:1a03] thread 2 ...
2013-11-22 17:37:09.138 Project2_OC[3191:1903] hello wolrd
2013-11-22 17:37:09.138 Project2_OC[3191:2503] thread 3 ...
2013-11-22 17:37:10.141 Project2_OC[3191:1903] hello wolrd
2013-11-22 17:37:10.141 Project2_OC[3191:2503] thread 3 ...
2013-11-22 17:37:10.141 Project2_OC[3191:1a03] thread 2 ...
2013-11-22 17:37:11.143 Project2_OC[3191:1903] hello wolrd
2013-11-22 17:37:11.143 Project2_OC[3191:2503] thread 3 ...
2013-11-22 17:37:11.143 Project2_OC[3191:1a03] thread 2 ...
2013-11-22 17:37:12.145 Project2_OC[3191:2503] thread 3 ...
2013-11-22 17:37:12.145 Project2_OC[3191:1a03] thread 2 ...
2013-11-22 17:37:12.145 Project2_OC[3191:1903] hello wolrd
2013-11-22 17:37:13.147 Project2_OC[3191:1903] hello wolrd
2013-11-22 17:37:13.147 Project2_OC[3191:1a03] thread 2 ...
2013-11-22 17:37:13.147 Project2_OC[3191:2503] thread 3 ...



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值