自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

剖析iPhone多線程

移動(dòng)開發(fā) iOS
這篇文章主要從線程創(chuàng)建與啟動(dòng)、線程的同步與鎖、線程的交互、線程池等等四個(gè)方面簡(jiǎn)單的講解一下iphone中的多線程編程。

iPhone多線程是本文要介紹的內(nèi)容,多線程在各種編程語(yǔ)言中都是難點(diǎn),很多語(yǔ)言中實(shí)現(xiàn)起來(lái)很麻煩,objective-c雖然源于c,但其多線程編程卻相當(dāng)簡(jiǎn)單,可以與java相媲美。這篇文章主要從線程創(chuàng)建與啟動(dòng)、線程的同步與鎖、線程的交互、線程池等等四個(gè)方面簡(jiǎn)單的講解一下iphone中的多線程編程。

一、線程創(chuàng)建與啟動(dòng)

線程創(chuàng)建主要有二種方式:

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

當(dāng)然,還有一種比較特殊,就是使用所謂的convenient method,這個(gè)方法可以直接生成一個(gè)線程并啟動(dòng)它,而且無(wú)需為線程的清理負(fù)責(zé)。這個(gè)方法的接口是:

  1. + (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument 

前兩種方法創(chuàng)建后,需要手機(jī)啟動(dòng),啟動(dòng)的方法是:

  1. - (void)start; 

二、線程的同步與鎖

要說(shuō)明線程的同步與鎖,***的例子可能就是多個(gè)窗口同時(shí)售票的售票系統(tǒng)了。我們知道在java中,使用synchronized來(lái)同步,而iphone雖然沒有提供類似java下的synchronized關(guān)鍵字,但提供了NSCondition對(duì)象接口。查看NSCondition的接口說(shuō)明可以看出,NSCondition是iphone下的鎖對(duì)象,所以我們可以使用NSCondition實(shí)現(xiàn)iphone中的線程安全。這是來(lái)源于網(wǎng)上的一個(gè)例子:

  1. SellTicketsAppDelegate.h 文件  
  2.  
  3. // SellTicketsAppDelegate.h  
  4. import <UIKit/UIKit.h> 
  5.  
  6. @interface SellTicketsAppDelegate : NSObject <UIApplicationDelegate> {  
  7.      int tickets;  
  8.      int count;  
  9.      NSThread* ticketsThreadone;  
  10.      NSThread* ticketsThreadtwo;  
  11.      NSCondition* ticketsCondition;  
  12.      UIWindow *window;  
  13. }  
  14. @property (nonatomic, retain) IBOutlet UIWindow *window;  
  15. @end  
  16.  
  17. SellTicketsAppDelegate.m 文件  
  18.  
  19. // SellTicketsAppDelegate.m  
  20. import "SellTicketsAppDelegate.h"  
  21. @implementation SellTicketsAppDelegate  
  22. @synthesize window;  
  23. - (void)applicationDidFinishLaunching:(UIApplication *)application {  
  24.      tickets = 100;  
  25.      count = 0;  
  26.      // 鎖對(duì)象  
  27.      ticketCondition = [[NSCondition alloc] init];  
  28.      ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];  
  29.      [ticketsThreadone setName:@"Thread-1"];  
  30.      [ticketsThreadone start];   
  31.      ticketsThreadtwo = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];  
  32.      [ticketsThreadtwo setName:@"Thread-2"];  
  33.      [ticketsThreadtwo start];  
  34.      //[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];  
  35.       // Override point for customization after application launch  
  36.      [window makeKeyAndVisible];   
  37.  
  38. }  
  39. - (void)run{  
  40.      while (TRUE) {  
  41.      // 上鎖  
  42.          [ticketsCondition lock];  
  43.          if(tickets > 0){  
  44.              [NSThread sleepForTimeInterval:0.5];  
  45.              count = 100 - tickets;  
  46.              NSLog(@"當(dāng)前票數(shù)是:%d,售出:%d,線程名:%@",tickets,count,[[NSThread currentThread] name]);  
  47.              tickets--;  
  48.          }else{  
  49.              break;  
  50.          }  
  51.          [ticketsCondition unlock];  
  52.      }  
  53. }  
  54. - (void)dealloc {  
  55. [ticketsThreadone release];  
  56.      [ticketsThreadtwo release];  
  57.      [ticketsCondition release];   
  58.      [window release];  
  59.      [super dealloc];  
  60. }  
  61. @end 

三、線程的交互

線程在運(yùn)行過程中,可能需要與其它線程進(jìn)行通信,如在主線程中修改界面等等,可以使用如下接口:

  1. - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait 

由于在本過程中,可能需要釋放一些資源,則需要使用NSAutoreleasePool來(lái)進(jìn)行管理,如:

  1. - (void)startTheBackgroundJob {  
  2.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  3.     // to do something in your thread job  
  4.     ...  
  5.     [self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];  
  6.     [pool release];  

小結(jié):剖析iPhone多線程的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來(lái)源: 博客園
相關(guān)推薦

2011-06-02 17:27:49

iphone 多線程

2011-07-08 16:43:46

iPhone Cocoa 多線程

2011-06-07 17:35:39

iphone 多線程

2010-02-04 10:19:39

C++多線程

2011-08-12 10:09:23

iPhone開發(fā)多線程

2011-08-09 14:24:18

iPhone多線程線程

2011-08-08 13:50:29

iPhone開發(fā) NSOperatio 多線程

2013-08-21 16:17:09

iPhone多線程

2011-08-01 12:53:25

iPhone 多線程 線程

2011-08-10 10:18:22

iPhone多線程線程

2011-07-21 15:20:31

iPhone SDK 多線程

2009-03-12 10:52:43

Java線程多線程

2023-06-05 07:56:10

線程分配處理器

2023-06-06 08:17:52

多線程編程Thread類

2013-07-16 10:12:14

iOS多線程多線程概念多線程入門

2021-12-26 18:22:30

Java線程多線程

2010-01-21 11:27:30

linux多線程機(jī)制線程同步

2009-06-29 17:49:47

Java多線程

2020-12-28 08:03:26

多線程進(jìn)程瀏覽器

2023-06-13 13:39:00

多線程異步編程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)