代码改变世界

【转】iphone开发之多线程NSThread和NSInvocationOperation

2013-03-17 21:26  maying_07  阅读(144)  评论(0编辑  收藏  举报

原文地址:http://www.cnblogs.com/lm3515/archive/2011/04/13/2015304.html

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

 1 @implementation MyCustomClass
 2 - (void)launchTaskWithData:(id)data
 3 {
 4     //创建一个NSInvocationOperation对象,并初始化到方法
 5     //在这里,selector参数后的值是你想在另外一个线程中运行的方法(函数,Method)
 6     //在这里,object后的值是想传递给前面方法的数据
 7     NSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:self
 8                     selector:@selector(myTaskMethod:) object:data];
 9 
10     // 下面将我们建立的操作“Operation”加入到本地程序的共享队列中(加入后方法就会立刻被执行)
11     // 更多的时候是由我们自己建立“操作”队列
12     [[MyAppDelegate sharedOperationQueue] addOperation:theOp];
13 }
14 
15 // 这个是真正运行在另外一个线程的“方法”
16 - (void)myTaskMethod:(id)data
17 {
18     // Perform the task.
19 }
20 
21 @end

 


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

 1 @interface MyViewController : UIViewController {
 2 
 3     NSOperationQueue *operationQueue;
 4     //在头文件中声明该队列
 5 }
 6 @end
 7 
 8 @implementation MyViewController
 9 
10 - (id)init 
11 {
12     self = [super init];
13     if (self) {
14         operationQueue = [[NSOperationQueue alloc] init]; //初始化操作队列
15         [operationQueue setMaxConcurrentOperationCount:1];
16         //在这里限定了该队列只同时运行一个线程
17         //这个队列已经可以使用了
18     }
19     return self;
20 }
21 
22 - (void)dealloc
23 {
24     [operationQueue release];
25     //正如Alan经常说的,我们是程序的好公民,需要释放内存!
26     [super dealloc];
27 }
28 
29 @end

 


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



///////////////////////////////////////////////////////////////////////////////////////////////////
1 在主线程里加入一个loading画面……
2 {
3 [window addSubview:view_loading];
4 [NSThread detachNewThreadSelector:@selector(init_backup:) toTarget:self withObject:nil];
5 }
可以通过performSelectorOhMainThread更新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,这个方法可以直接生成一个线程并启动它,而且无需为线程的清理负责。

 1 #import <UIKit/UIKit.h>
 2 @interface SellTicketsAppDelegate : NSObject <UIApplicationDelegate> {
 3     int tickets;
 4     int count;
 5     NSThread* ticketsThreadone;
 6     NSThread* ticketsThreadtwo;
 7     NSCondition* ticketsCondition;
 8     UIWindow *window;
 9 }
10 
11 @property (nonatomic, retain) IBOutlet UIWindow *window;
12 @end

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

 1 //  SellTicketsAppDelegate.m
 2 //  SellTickets
 3 //
 4 //  Created by sun dfsun2009 on 09-11-10.
 5 //  Copyright __MyCompanyName__ 2009. All rights reserved.
 6 //
 7 #import "SellTicketsAppDelegate.h"
 8 @implementation SellTicketsAppDelegate
 9 @synthesize window;
10 - (void)applicationDidFinishLaunching:(UIApplication *)application {
11     tickets = 100;
12     count = 0;
13     // 锁对象
14     ticketCondition = [[NSCondition alloc] init];
15     ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
16     [ticketsThreadone setName:@"Thread-1"];
17     [ticketsThreadone start];
18     ticketsThreadtwo = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
19     [ticketsThreadtwo setName:@"Thread-2"];
20     [ticketsThreadtwo start];
21     //[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
22     // Override point for customization after application launch
23     [window makeKeyAndVisible];
24 }
25 - (void)run{
26     while (TRUE) {
27         // 上锁
28         [ticketsCondition lock];
29         if(tickets > 0)
30         {
31             [NSThread sleepForTimeInterval:0.5];
32             count = 100 - tickets;
33             NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThread currentThread] name]);
34             tickets--;
35         }else
36         {
37             break;
38         }
39         [ticketsCondition unlock];
40     }
41 }
42 -         (void)dealloc {
43     [ticketsThreadone release];
44     [ticketsThreadtwo release];
45     [ticketsCondition release];
46     [window release];
47     [super dealloc];
48 }
49 @end
1 // 定义
2 #import <UIKit/UIKit.h>
3 
4 @interface ThreadSyncSampleViewController : UIViewController {
5 int _threadCount;
6 NSCondition *_myCondition;
7 }
8 
9 @end
  1 //实现文件如下:
  2 
  3 #import "ThreadSyncSampleViewController.h"
  4 
  5 @implementation ThreadSyncSampleViewController
  6 
  7 /*
  8 // The designated initializer. Override to perform setup that is required before the view is loaded.
  9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 10     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 11         // Custom initialization
 12     }
 13     return self;
 14 }
 15 */
 16 
 17 /*
 18 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 19 - (void)loadView {
 20 }
 21 */
 22 
 23 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 24 - (void)viewDidLoad {
 25     [super viewDidLoad];
 26 //
 27 //_myCondition = nil;
 28 //
 29 _myCondition = [[NSCondition alloc] init];
 30 //
 31 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:30
 32              target:self
 33               selector:@selector(threadTester)
 34               userInfo:nil
 35              repeats:YES];
 36 [timer fire];
 37 
 38 }
 39 
 40 
 41 - (void)threadTester{
 42 [_myCondition lock];
 43 
 44 _threadCount = -2;
 45 //如果有n个要等待的thread,这里置成 -n
 46 [_myCondition unlock];
 47 //
 48 NSLog(@"");
 49 NSLog(@"------------------------------------------------------------------------------");
 50 [NSThread detachNewThreadSelector:@selector(threadOne) toTarget:self withObject:nil];
 51 [NSThread detachNewThreadSelector:@selector(threadTwo) toTarget:self withObject:nil];
 52 [NSThread detachNewThreadSelector:@selector(threadThree) toTarget:self withObject:nil];
 53 return;
 54 }
 55 
 56 - (void)threadOne{
 57 NSLog(@"@@@ In thread 111111 start.");
 58 [_myCondition lock];
 59 
 60 int n = rand()%5 + 1;
 61 NSLog(@"@@@ Thread 111111 Will sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
 62 sleep(n);
 63 //[NSThread sleepForTimeInterval:n];
 64 _threadCount ++ ;
 65 NSLog(@"@@@ Thread 111111 has sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
 66 [_myCondition signal];
 67 NSLog(@"@@@ Thread 1111111 has signaled ,now _threadCount is : %d",_threadCount);
 68 [_myCondition unlock];
 69 NSLog(@"@@@ In thread one complete.");
 70 [NSThread exit];
 71 return;
 72 }
 73 
 74 - (void)threadTwo{
 75 NSLog(@"### In thread 2222222 start.");
 76 [_myCondition lock];
 77 
 78 int n = rand()%5 + 1;
 79 NSLog(@"### Thread 2222222 Will sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
 80 sleep(n);
 81 //   [NSThread sleepForTimeInterval:n];
 82 _threadCount ++ ;
 83 NSLog(@"### Thread 2222222 has sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
 84 [_myCondition signal];
 85 NSLog(@"### Thread 2222222 has signaled ,now _threadCount is : %d",_threadCount);
 86 [_myCondition unlock];
 87 //_threadCount ++ ;
 88 NSLog(@"### In thread 2222222 complete.");
 89 [NSThread exit];
 90 return;
 91 }
 92 
 93 - (void)threadThree{
 94 NSLog(@"<<< In thread 333333 start.");
 95 [_myCondition lock];
 96 while (_threadCount < 0) {
 97   [_myCondition wait];
 98 }
 99 NSLog(@"<<< In thread 333333 ,_threadCount now is %d ,will start work.",_threadCount);
100 [_myCondition unlock];
101 NSLog(@"<<< In thread 333333 complete.");
102 [NSThread exit];
103 return;
104 }
105 
106 /*
107 // Override to allow orientations other than the default portrait orientation.
108 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
109     // Return YES for supported orientations
110     return (interfaceOrientation == UIInterfaceOrientationPortrait);
111 }
112 */
113 
114 - (void)didReceiveMemoryWarning {
115 // Releases the view if it doesn't have a superview.
116     [super didReceiveMemoryWarning];
117 
118 // Release any cached data, images, etc that aren't in use.
119 }
120 
121 - (void)viewDidUnload {
122 // Release any retained subviews of the main view.
123 // e.g. self.myOutlet = nil;
124 }
125 
126 
127 - (void)dealloc {
128 [_myCondition release];
129     [super dealloc];
130 }
131 
132 @end