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

iOS多線程初體驗(yàn)

移動(dòng)開發(fā) iOS
iPhone中的線程應(yīng)用并不是無節(jié)制的,官方給出的資料顯示iPhone OS下的主線程的堆棧大小是1M,第二個(gè)線程開始都是512KB。并且該值不能通過編譯器開關(guān)或線程API函數(shù)來更改。

iOS多線程初體驗(yàn)是本文要介紹的內(nèi)容,iPhone中的線程應(yīng)用并不是無節(jié)制的,官方給出的資料顯示iPhone OS下的主線程的堆棧大小是1M,第二個(gè)線程開始都是512KB。并且該值不能通過編譯器開關(guān)或線程API函數(shù)來更改。只有主線程有直接修改UI的能力。

一、 NSOperation和NSOperationQueue

1、一個(gè)繼承自  NSOperation的操作類,該類的實(shí)現(xiàn)中必須有 - (void)main方法的。

2、使用NSOperation的最簡(jiǎn)單方法就是將其放入NSOperationQueue中。

一旦一個(gè)操作被加入隊(duì)列,該隊(duì)列就會(huì)啟動(dòng)并開始處理它(即調(diào)用該操作類的main方法)。一旦該操作完成隊(duì)列就會(huì)釋放它。

  1.      self.queue = [[NSOperationQueue alloc] init];  
  2.       ArticleParseOperation *parser = [[ArticleParseOperation alloc] initWithData:filePath delegate:self];  
  3.       [queue addOperation:parser];  
  4.       [parser release];  
  5.      [queue release]; 

3、可以給操作隊(duì)列設(shè)置最多同事運(yùn)行的操作數(shù): [queue setMaxConcurrentOperationCount:2];

二、NSThread

1、線程創(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)它,而且無需為線程的清理負(fù)責(zé)。這個(gè)方法的接口是:

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

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

  1. - (void)start; 

2、線程的同步與鎖

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

SellTicketsAppDelegate.h 文件

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

三、線程的交互

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

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

由于在本過程中,可能需要釋放一些資源,則需要使用NSAutoreleasePool來進(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];  

如果你什么都不考慮,在線程函數(shù)內(nèi)調(diào)用 autorelease 、那么會(huì)出現(xiàn)下面的錯(cuò)誤:

  1. NSAutoReleaseNoPool(): Object 0x********* of class NSConreteData autoreleased with no pool in place …. 

四、關(guān)于線程池,大家可以查看NSOperation的相關(guān)資料。

小結(jié):iOS多線程初體驗(yàn)的內(nèi)容介紹完了,希望本文對(duì)你有所幫助。更多關(guān)于IOS的內(nèi)容請(qǐng)參考編輯推薦。

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-06-07 17:35:39

iphone 多線程

2021-08-12 14:33:20

Python多線程編程

2011-06-24 11:03:31

Qt 多線程 線程

2023-10-06 23:06:01

多線程Python

2018-03-22 15:36:26

程序員RubyiOS

2009-03-09 15:12:39

XenServer安裝

2009-08-01 09:06:35

UbuntuOneLinux開源操作系統(tǒng)

2013-12-12 10:46:22

2013-12-12 11:33:31

iOS 7API

2023-07-15 08:01:38

2015-07-22 09:51:51

iOS開發(fā)線程

2011-08-09 10:51:36

Xcode 4iosSDK

2011-05-30 15:12:10

App Invento 初體驗(yàn)

2010-11-22 10:31:17

Sencha touc

2013-07-16 10:12:14

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

2015-07-22 09:39:38

IOS多線程同步

2016-04-12 09:48:24

nsthread多線程ios

2011-09-15 15:03:10

2009-11-30 10:09:02

谷歌Chrome OS

2010-12-13 11:39:39

點(diǎn)贊
收藏

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