IOS多线程有三种主要方法


(1)NSThread


(2)NSOperation


(3)GCD


下面简单介绍这三个方法 



1.NSThread


调用方法如下: 


如函数需要输入参数,可从object传进去。


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


    (2) NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadInMainMethod:) object:nil];


 [myThread start]; 


    (3) [obj performSelectorInBackground:@selector(threadMe) withObject:nil];


 


提个问题:如果某个ViewController里运行了一个Thread,Thread还没结束的时候,这个ViewController被Release了,结果会如何? 

经过的的测试,Thread不结束,ViewController一直保留,不会执行dealloc方法。

 

我的线程安全解决方案 :


在NSOperation.m中的main函数中 执行回调函数之前会询问 [self isCancled]

既然阻止不了线程执行 只能想办法 让回调函数不执行 

在创建operation 得对象的 dealloc方法中 

首先 手动 [operation canel]

然后         [operation release]

2.NSOperation  



NSoperation也是多线程的一种,NSopertaion有2种形式


(1) 并发执行


       并发执行你需要重载如下4个方法


     //执行任务主函数,线程运行的入口函数


    - (void)start 


       //是否允许并发,返回YES,允许并发,返回NO不允许。默认返回NO


    -(BOOL)isConcurrent 


    - (BOOL)isExecuting


     //是否已经完成,这个必须要重载,不然放在放在NSOperationQueue里的NSOpertaion不能正常释放。


   - (BOOL)isFinished


   


   比如TestNSOperation:NSoperaion 重载上述的4个方法,


   声明一个NSOperationQueue, NSOperationQueue *queue = [[[NSOperationQueue alloc ] init] autorelease];


  [queue addOperation:testNSoperation];


  它会自动调用TestNSOperation里的 start函数,如果需要多个NSOperation,你需要设置queue的一些属性,如果多个NSOperation之间又依赖关系,也可以设置,具体可以参考API 文档。 

 



(2)非并发执行


  -(void)main


   只需要重载这个main方法就可以了。  


 


3.GCD

 

 

GCD是和block紧密相连的,所以最好先了解下block(可以查看这里).GCD是C level的函数,这意味着它也提供了C的函数指针作为参数,方便了C程序员.

下面首先来看GCD的使用:

 




1




​dispatch_async(dispatch_queue_t queue, dispatch_block_t block);​



 

async表明异步运行,block代表的是你要做的事情,queue则是你把任务交给谁来处理了.(除了async,还有sync,delay,本文以async为例).

之所以程序中会用到多线程是因为程序往往会需要读取数据,然后更新UI.为了良好的用户体验,读取数据的操作会倾向于在后台运行,这样以避免阻塞主线程.GCD里就有三种queue来处理。

1. Main queue:

  顾名思义,运行在主线程,由dispatch_get_main_queue获得.和ui相关的就要使用Main Queue.

2.Serial quque(private dispatch queue)

  每次运行一个任务,可以添加多个,执行次序FIFO. 通常是指程序员生成的,比如:

 




1


2


3


4




​NSDate *da = [NSDate date];​


​NSString *daStr = [da description];​


​const​​ ​​char​​ ​​*queueName = [daStr UTF8String];​


​dispatch_queue_t myQueue = dispatch_queue_create(queueName, NULL);​



3. Concurrent queue(global dispatch queue):

 

可以同时运行多个任务,每个任务的启动时间是按照加入queue的顺序,结束的顺序依赖各自的任务.使用dispatch_get_global_queue获得.

所以我们可以大致了解使用GCD的框架:

 




1


2


3


4


5


6


7




​dispatch_async(getDataQueue,^{​


​//获取数据,获得一组后,刷新UI.​


​dispatch_aysnc (mainQueue, ^{​


​//UI的更新需在主线程中进行​


​};​


​}​


​)​



由此可见,GCD的使用非常简单,以我的使用经验来看,以后会逐步淘汰使用NSOperation而改用GCD。

 



多线程编程是防止主线程堵塞,增加运行效率等等的最佳方法。而原始的多线程方法存在很多的毛病,包括线程锁死等。在Cocoa中,Apple提供了NSOperation这个类,提供了一个优秀的多线程编程方法。


关于IOS多线程的简单总结_主线程


本次介绍NSOperation的子集,简易方法的NSInvocationOperation: 

@implementation MyCustomClass 

- (void)launchTaskWithData:(id)data 

//创建一个NSInvocationOperation对象,并初始化到方法 

//在这里,selector参数后的值是你想在另外一个线程中运行的方法(函数,Method) 

//在这里,object后的值是想传递给前面方法的数据 

NSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:self 

selector:@selector(myTaskMethod:) object:data];

// 下面将我们建立的操作“Operation”加入到本地程序的共享队列中(加入后方法就会立刻被执行) 

// 更多的时候是由我们自己建立“操作”队列 

[[MyAppDelegate sharedOperationQueue] addOperation:theOp]; 

}

// 这个是真正运行在另外一个线程的“方法” 

- (void)myTaskMethod:(id)data 

// Perform the task. 

}

@end一个NSOperationQueue 操作队列,就相当于一个线程管理器,而非一个线程。因为你可以设置这个线程管理器内可以并行运行的的线程数量等等。下面是建立并初始化一个操作队列:

@interface MyViewController : UIViewController {

NSOperationQueue *operationQueue; 

//在头文件中声明该队列 

@end

@implementation MyViewController

- (id)init 

self = [super init]; 

if (self) { 

operationQueue = [[NSOperationQueue alloc] init]; //初始化操作队列 

[operationQueue setMaxConcurrentOperationCount:1]; 

//在这里限定了该队列只同时运行一个线程 

//这个队列已经可以使用了 

return self; 

}

- (void)dealloc 

[operationQueue release]; 

//正如Alan经常说的,我们是程序的好公民,需要释放内存! 

[super dealloc]; 

}

@end简单介绍之后,其实可以发现这种方法是非常简单的。很多的时候我们使用多线程仅仅是为了防止主线程堵塞,而NSInvocationOperation就是最简单的多线程编程,在iPhone编程中是经常被用到的。

 

///////////////////////////////////////////////////////////////////////////////////////////////////

1 在主线程里加入一个loading画面…… 

2 { 

3 [window addSubview:view_loading]; 

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

5 } 

可以通过performSelectorOnMainThread更新UI元素,比如设置进度条等等。最后消除loading画面,载入主View。 

7 - (void)init_backup:(id)sender 

8 { 

9 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

10 

11 // ... 

12 int i = status; 

13 [self performSelectorOnMainThread:@selector(show_loading:) withObject:[NSNumber numberWithInt:i] waitUntil Done:NO]; 

14 

15 [view_loading removeFromSuperview]; 

16 [window addSubview:tabcontroller_main.view]; 

17 [pool release]; 

18 }

///////////////////////////////////////////////////////

利用iphone的多线程实现和线程同步

从接口的定义中可以知道,NSThread和大多数iphone的接口对象一样,有两种方式可以初始化:

一种使用initWithTarget:(id)target selector:(SEL)selector object:(id)argument,但需要负责在对象的retain count为0时调用对象的release方法清理对象。

另一种则使用所谓的convenient method,这个方便接口就是detachNewThreadSelector,这个方法可以直接生成一个线程并启动它,而且无需为线程的清理负责。

#import <UIKit/UIKit.h> 

@interface SellTicketsAppDelegate : NSObject <UIApplicationDelegate> { 

int tickets; 

int count; 

NSThread* ticketsThreadone; 

NSThread* ticketsThreadtwo; 

NSCondition* ticketsCondition; 

UIWindow *window; 

}

@property (nonatomic, retain) IBOutlet UIWindow *window; 

@end

然后在实现中添加如下代码: 

// SellTicketsAppDelegate.m 

// SellTickets 

// 

// 

#import "SellTicketsAppDelegate.h" 

@implementation SellTicketsAppDelegate 

@synthesize window; 

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

tickets = 100; 

count = 0; 

// 锁对象 

ticketCondition = [[NSCondition alloc] init]; 

ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; 

[ticketsThreadone setName:@"Thread-1"]; 

[ticketsThreadone start]; 

ticketsThreadtwo = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; 

[ticketsThreadtwo setName:@"Thread-2"]; 

[ticketsThreadtwo start]; 

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

// Override point for customization after application launch 

[window makeKeyAndVisible]; 

- (void)run{ 

while (TRUE) { 

// 上锁 

[ticketsCondition lock]; 

if(tickets > 0) 

[NSThread sleepForTimeInterval:0.5]; 

count = 100 - tickets; 

NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThread currentThread] name]); 

tickets--; 

}else 

break; 

[ticketsCondition unlock]; 

- (void)dealloc { 

[ticketsThreadone release]; 

[ticketsThreadtwo release]; 

[ticketsCondition release]; 

[window release]; 

[super dealloc]; 

@end

------------------------------------------------------------------------------------- 

// 定义 

#import <UIKit/UIKit.h>

@interface ThreadSyncSampleViewController : UIViewController { 

int _threadCount; 

NSCondition *_myCondition; 

}

@end

//实现文件如下:

#import "ThreadSyncSampleViewController.h"

@implementation ThreadSyncSampleViewController

/* 

// The designated initializer. Override to perform setup that is required before the view is loaded. 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 

if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 

// Custom initialization 

return self; 

*/

/* 

// Implement loadView to create a view hierarchy programmatically, without using a nib. 

- (void)loadView { 

*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 

- (void)viewDidLoad { 

[super viewDidLoad]; 

// 

//_myCondition = nil; 

// 

_myCondition = [[NSCondition alloc] init]; 

// 

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:30 

target:self 

selector:@selector(threadTester) 

userInfo:nil 

repeats:YES]; 

[timer fire];

}

- (void)threadTester{ 

[_myCondition lock];

_threadCount = -2; 

//如果有n个要等待的thread,这里置成 -n 

[_myCondition unlock]; 

// 

NSLog(@""); 

NSLog(@"------------------------------------------------------------------------------"); 

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

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

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

return; 

}

- (void)threadOne{ 

NSLog(@"@@@ In thread 111111 start."); 

[_myCondition lock];

int n = rand()%5 + 1; 

NSLog(@"@@@ Thread 111111 Will sleep %d seconds ,now _threadCount is : %d",n,_threadCount); 

sleep(n); 

//[NSThread sleepForTimeInterval:n]; 

_threadCount ++ ; 

NSLog(@"@@@ Thread 111111 has sleep %d seconds ,now _threadCount is : %d",n,_threadCount); 

[_myCondition signal]; 

NSLog(@"@@@ Thread 1111111 has signaled ,now _threadCount is : %d",_threadCount); 

[_myCondition unlock]; 

NSLog(@"@@@ In thread one complete."); 

[NSThread exit]; 

return; 

}

- (void)threadTwo{ 

NSLog(@"### In thread 2222222 start."); 

[_myCondition lock];

int n = rand()%5 + 1; 

NSLog(@"### Thread 2222222 Will sleep %d seconds ,now _threadCount is : %d",n,_threadCount); 

sleep(n); 

// [NSThread sleepForTimeInterval:n]; 

_threadCount ++ ; 

NSLog(@"### Thread 2222222 has sleep %d seconds ,now _threadCount is : %d",n,_threadCount); 

[_myCondition signal]; 

NSLog(@"### Thread 2222222 has signaled ,now _threadCount is : %d",_threadCount); 

[_myCondition unlock]; 

//_threadCount ++ ; 

NSLog(@"### In thread 2222222 complete."); 

[NSThread exit]; 

return; 

}

- (void)threadThree{ 

NSLog(@"<<< In thread 333333 start."); 

[_myCondition lock]; 

while (_threadCount < 0) { 

[_myCondition wait]; 

NSLog(@"<<< In thread 333333 ,_threadCount now is %d ,will start work.",_threadCount); 

[_myCondition unlock]; 

NSLog(@"<<< In thread 333333 complete."); 

[NSThread exit]; 

return; 

}

/* 

// Override to allow orientations other than the default portrait orientation. 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

// Return YES for supported orientations 

return (interfaceOrientation == UIInterfaceOrientationPortrait); 

*/

- (void)didReceiveMemoryWarning { 

// Releases the view if it doesn't have a superview. 

[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use. 

}

- (void)viewDidUnload { 

// Release any retained subviews of the main view. 

// e.g. self.myOutlet = nil; 

}

- (void)dealloc { 

[_myCondition release]; 

[super dealloc]; 

}

@end