iphone ios 多线程 multi thread编程汇总

以下大家友情支持一下:

做了一个产品,需要人气支持一下,android和iphone上91市场搜索#super junior粉丝团#,或者直接到页面下载http://m.ixingji.com/m.html?p=X16,大家帮忙捧捧场


老规矩,先贴2篇e文的官方文章

1)Threading Programming Guide

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html#//apple_ref/doc/uid/10000057i-CH15-SW7

2)Concurrency(并发) Programming Guide

http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/ConcurrencyandApplicationDesign/ConcurrencyandApplicationDesign.html#//apple_ref/doc/uid/TP40008091-CH100-SW6


这两个有区别,如果各位e文好,又有耐心,就滤过下面的。

推荐

1)[Cocoa]深入浅出 Cocoa 之多线程 NSThread

http://blog.csdn.net/kesalin/article/details/6698146

2)利用iphone的多线程实现“售票系统”(手把手教你iphone开发 - 基础篇)

http://blog.csdn.net/dongfengsun/article/details/4794010

3)Concurrency 学习 (Mac & iphone) (一)

http://hi.baidu.com/songxiaoweiss/blog/item/699971d7c6c579de50da4b51.html

4)Concurrency 学习 (Mac & iphone) (二)

http://hi.baidu.com/songxiaoweiss/blog/item/82b08d13912cb4dca6ef3f29.html


找了很多网上关于ios多线程编码的文章,基本都是说到3个方法

1)detachNewThreadSelector或performSelectorInBackground

// NSThread中的方法,启动线程Detaches a new thread and uses the specified selector as the thread entry point.
+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument
// NSObject中的方法,启动线程Invokes a method of the receiver on a new background thread.
- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg
// NSObject中的方法,因为更新UI只能是主线程中执行。所以这个函数主要用于子线程调用主线程中的aSelector,来更新主线程的UI或参数。
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

很经典的例子,主线程要更新列表图像,需要连接网络下载:

[1]主线程调用

	[NSThread detachNewThreadSelector:@selector(downloadimg:) toTarget:self withObject:url];

[2]线程下载完毕后,通知主线程,并更新列表图像。

        [self performSelectorOnMainThread:@selector(stateIsLoading) withObject:nil waitUntilDone:NO];

2)常规军,NSThread

- (id)init;	// designated initializer
- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;

这个例子我copy自网上,要说明线程的同步与锁,最好的例子可能就是多个窗口同时售票的售票系统了。我们知道在java中,使用synchronized来同步,而iphone虽然没有提供类似java下的synchronized关键字,但提供了NSCondition对象接口。查看NSCondition的接口说明可以看出,NSCondition是iphone下的锁对象,所以我们可以使用NSCondition实现iphone中的线程安全。

//  SellTicketsAppDelegate.h
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
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

注意,线程initWithTarget初始化完毕后,要调用start来启动他。


“这两种方式的区别是:前一种一调用就会立即创建一个线程来做事情;而后一种虽然你 alloc 了也 init了,但是要直到我们手动调用 start 启动线程时才会真正去创建线程。这种延迟实现思想在很多跟资源相关的地方都有用到。后一种方式我们还可以在启动线程之前,对线程进行配置,比如设置 stack 大小,线程优先级。”


3)线程队列NSOperationQueue

可以参考文章使用NSOperationQueue简化多线程开发

http://marshal.easymorse.com/archives/4519






你可能感兴趣的:(thread,多线程,ios,编程,iPhone,concurrency)