用NSThread创建子线程

关于线程的原理和优缺点在这里我就不写了,因为我也实在弄不清楚。由于Mac OS是基于Unix的内核搭建的操作系统,所以基本的POSIX标准肯定是支持的,我们看官方手册上也提到我们可以放心使用POSIX的线程编程。

除了POSIX一种选择外,苹果提供了NSThread类也可以实现多线程编程。使用NSThread创建线程有下面两种方式

  • 使用detachNewThreadSelector:toTarget:withObject: 类的方法创建新线程
  • 创建一个新的NSThread实例,调用start方法(只在iOS以及Max OS X10.5及以上版本)

第一种方法在Max OS中使用历史比较悠久了,很多项目中都可以看到这样的代码。

#import <Foundation/Foundation.h>

@interface MyThread : NSObject
{
		
}

-(void)start:(int)times;
-(void)work:(NSNumber *)times;

@end

@implementation MyThread

//创建子线程
-(void)start:(int)times
{
	//创建方法一
	[NSThread detachNewThreadSelector:@selector(work:) toTarget:self withObject:[NSNumber numberWithInt:times]];
	
	//创建方法二
	/*	
	NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(work:) 
												 object:[NSNumber numberWithInt:times]];
	[thread start];
	 */
}

//子线程入口函数
-(void)work:(NSNumber *)times
{
	int i = 0;
	for(i = 0;i < [times intValue];i ++)
	{
		[NSThread sleepForTimeInterval:0.5];
		printf("child thread %d\n",i);
	}
}

@end

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	//创建子线程工作的实例
	MyThread *mt = [[[MyThread alloc] init] autorelease];
	[mt start:5];
	
	//主线程代码
	int i = 0;
	for(i = 0;i < 10;i ++)
	{
		[NSThread sleepForTimeInterval:0.5];
		printf("main thread %d\n",i);
	}
	
	[pool drain];
    return 0;
}

运行结果如下所示

main thread 0
child thread 0
main thread 1
child thread 1
child thread 2
main thread 2
main thread 3
child thread 3
child thread 4
main thread 4
main thread 5
main thread 6
main thread 7
main thread 8
main thread 9
此外还有一种创建方法,就是使用NSObject的performSelectorInBackground:withObject方法来创建线程,只需要将上面main函数中创建子线程工作的实例部分替换为如下代码即可。

	//创建子线程工作的实例
	MyThread *mt = [[[MyThread alloc] init] autorelease];
	[mt performSelectorInBackground:@selector(work:) withObject:[NSNumber numberWithInt:5]];

当然如今流行的并发编程不再是多线程了,在windows,linux平台我们都了解到各种异步编程模型了,Mac OS系统中也提供了强大的NSOperationQueue,我们只需要定义自己的工作内容,投递给操作系统的线程池帮我们处理即可。代码如下:

#import <Foundation/Foundation.h>

//类定义
@interface MyWork : NSOperation
{
    int times;
}

@property int times;

-(void)main;

@end

//类实现
@implementation MyWork

@synthesize times; 

-(void)main
{
    int i;
    
    for (i=0; i<times; i++) {
        [NSThread sleepForTimeInterval:0.5];
        NSLog(@"child operation running at %d",i);
    }
}

@end

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
    //创建“线程池”
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    MyWork *work = [[[MyWork alloc] init] autorelease];
    work.times = 5;
    
    //添加操作
    [queue addOperation:work];
    
    int i;
    for (i=0; i<10; i++) {
        [NSThread sleepForTimeInterval:0.5];
        NSLog(@"main operation running at %d",i);
    }
    
    [queue release];
	[pool drain];
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值