iPhone开发之多线程使用
2012-05-13 14:46
316 查看
创建线程有三种方法:
一、通过[NSThread detachNewThreadSelector:@selector(addAction) toTarget:self withObject:nil]创建,无具体的返回对象,线程不受用户控制,控制权掌握在系统的手中;
二、通过[[NSThread alloc] initWithTarget:self selector:@selector(addNumberTo:) object:maxNumber]创建,通过该方式创建的线程由用户自己管理;
三、自定义线程,通过继承NSThread来实现。
自定义的线程类:
myThread.h
myThread.m
一、通过[NSThread detachNewThreadSelector:@selector(addAction) toTarget:self withObject:nil]创建,无具体的返回对象,线程不受用户控制,控制权掌握在系统的手中;
二、通过[[NSThread alloc] initWithTarget:self selector:@selector(addNumberTo:) object:maxNumber]创建,通过该方式创建的线程由用户自己管理;
三、自定义线程,通过继承NSThread来实现。
// // ViewController.m // ThreadDemo // // Created by Fox on 12-5-13. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "ViewController.h" #import "myThread.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建子线程1,使用detachNewThreadSelector无任何返回对象供操作,用户无法管理生成的线程,管理权为系统所有 [NSThread detachNewThreadSelector:@selector(addAction) toTarget:self withObject:nil]; //创建子线程2,线程状态由用户来管理 NSNumber *maxNumber = [[NSNumber alloc] initWithInt:120]; NSThread *mThread = [[NSThread alloc] initWithTarget:self selector:@selector(addNumberTo:) object:maxNumber]; [mThread start];//启动线程 //创建子线程3,通过用户自定义的方式创建 myThread *_thread = [[myThread alloc] init]; [_thread start]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } - (void)addAction{ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; int number = -1; int sum = 0; while (number++ <= 100) { sum += number; } NSLog(@"子线程1执行,the number is:%d",sum); [pool release]; } - (void)addNumberTo:(NSNumber *)maxNumber{ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; int number = -1; int sum = 0; while (number++ <= [maxNumber intValue]) { sum += number; } NSLog(@"子线程2执行,the number is:%d",sum); [pool release]; } @end
自定义的线程类:
myThread.h
#import <Foundation/Foundation.h> @interface myThread : NSThread @end
myThread.m
// // myThread.m // ThreadDemo // // Created by Fox on 12-5-13. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "myThread.h" @implementation myThread //重写线程的入口函数,在此添加线程方法 - (void)main{ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; int number = -1; int sum = 0; while (number++ <= 100) { sum += number; } NSLog(@"子线程3执行,the number is:%d",sum); [pool release]; } @end
相关文章推荐
- iPhone开发进阶(11)— 多线程的使用与注意事项
- iPhone开发进阶(11)--- 多线程的使用与注意事项
- iPhone开发进阶(11)--- 多线程的使用与注意事项
- iPhone开发进阶(11)--- 多线程的使用与注意事项
- iphone开发使用NSThread和NSInvocationOperation实现多线程
- iPhone开发 多线程的使用
- iPhone开发多线程使用与注意事项
- iPhone开发进阶(11)--- 多线程的使用与注意事项
- iPhone开发进阶(11)--- 多线程的使用与注意事项
- 【转载】 iphone使用NSOperationQueue简化多线程开发
- iPhone开发多线程使用与注意事项
- 我的iphone开发学习笔记(三)使用UISlide, UIImageView及动画
- C#多线程开发:使用lock语句同步多个线程
- iPhone开发之深入浅出 (2) — ARC之@property使用
- iPhone网络开发之如何使用NSURLConnection(一)
- iPhone开发【十四】多线程开发之NSThread——子线程模拟耗时操作
- 在iOS开发为什么使用多线程 ,多线程有哪些方法。
- Windows Mobile使用Native C++开发多线程程序
- iPhone开发之SQLite使用详解
- iphone开发中使用动态库(dylib)和动态加载framework (获取iphone的IMSI和设置飞行模式)