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

iPhone開發(fā)多線程使用與注意事項

移動開發(fā) iOS
本文介紹的是iPhone開發(fā)多線程使用與注意事項,講解iphone中的多線程的使用,先來看內(nèi)容詳解。

iPhone開發(fā)多線程使用與注意事項是本文要介紹的內(nèi)容,主要介紹一下iPhone SDK中多線程的使用方法以及注意事項。雖然現(xiàn)在大部分PC應(yīng)用程序都支持多線程/多任務(wù)的開發(fā)方式,但是在iPhone上,Apple并不推薦使用多線程的編程方式。

但是多線程編程畢竟是發(fā)展的趨勢,而且據(jù)說即將推出的iPhone OS4將全面支持多線程的處理方式。所以說掌握多線程的編程方式,在某些場合一定能挖掘出iPhone的更大潛力。

從例子入手

先從一個例程入手,具體的代碼參考了這里。還有例程可以下載。

多線程程序的控制模型可以參考這里,一般情況下都是使用 管理者/工人模型, 這里,我們使用iPhone SDK中的 NSThread 來實現(xiàn)它。

首先創(chuàng)建一個新的 View-based application 工程,名字為 "TutorialProject" 。界面如下圖所示,使用UILabel實現(xiàn)兩部分的Part(Thread Part和Test Part),Thread Part中包含一個UIProgressView和一個UIButton;而Test Part包含一個值和一個UISlider,如圖:

iPhone開發(fā)多線程使用與注意事項

接下來,在 TutorialProjectViewController.h 文件中創(chuàng)建各個UI控件的 IBOutlets.

  1. @interface TutorialProjectViewController : UIViewController {  
  2.     // ------ Tutorial code starts here ------  
  3.     // Thread part  
  4.     IBOutlet UILabel *threadValueLabel;  
  5.     IBOutlet UIProgressView *threadProgressView;  
  6.     IBOutlet UIButton *threadStartButton;  
  7.     // Test part  
  8.     IBOutlet UILabel *testValueLabel;  
  9.     // ------ Tutorial code ends here ------  

同時,也需要創(chuàng)建outlets變量的property.

  1. @property (nonatomic, retain) IBOutlet UILabel *threadValueLabel;  
  2. @property (nonatomic, retain) IBOutlet UIProgressView *threadProgressView;  
  3. @property (nonatomic, retain) IBOutlet UIProgressView *threadStartButton;  
  4. @property (nonatomic, retain) IBOutlet UILabel *testValueLabel; 

接下來定義按鈕按下時的動作函數(shù),以及slider的變化函數(shù)。

  1. - (IBAction) startThreadButtonPressed:(UIButton *)sender;   
  2. - (IBAction) testValueSliderChanged:(UISlider *)sender; 

然后在TutorialProjectViewController.m 文件中synthesize outlets,并在文件為實現(xiàn)dealloc釋放資源。

  1. @synthesize threadValueLabel, threadProgressView, testValueLabel, threadStartButton;  
  2. ...  
  3. - (void)dealloc {  
  4.     // ------ Tutorial code starts here ------  
  5.     [threadValueLabel release];  
  6.     [threadProgressView release];  
  7.     [threadStartButton release];  
  8.     [testValueLabel release];  
  9.     // ------ Tutorial code ends here ------  
  10.     [super dealloc];  

現(xiàn)在開始線程部分的代碼,首先當(dāng) thread button 被按下的時候,創(chuàng)建新的線程.

  1. - (IBAction) startThreadButtonPressed:(UIButton *)sender {  
  2.     threadStartButton.hidden = YES;  
  3.     threadValueLabel.text = @"0";  
  4.     threadProgressView.progress = 0.0;  
  5.     [NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];  

該按鈕被按下后,隱藏按鈕以禁止多次創(chuàng)建線程。然后初始化顯示值和進度條,最后創(chuàng)建新的線程,線程的函數(shù)為 startTheBackgroundJob.

具體的 startTheBackgroundJob 函數(shù)定義如下.

  1. - (void)startTheBackgroundJob {  
  2.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  3.     // 線程開始后先暫停3秒(這里只是演示暫停的方法,你不是必須這么做的)  
  4.     [NSThread sleepForTimeInterval:3];  
  5.     [self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];  
  6.     [pool release];  

在第1行,創(chuàng)建了一個 NSAutoreleasePool 對象,用來管理線程中自動釋放的對象資源。這里 NSAutoreleasePool 在線程退出的時候釋放。這符合 Cocoa GUI 應(yīng)用程序的一般規(guī)則。

#p#

最后一行,阻塞調(diào)用(waitUntilDone狀態(tài)是ON)函數(shù) makeMyProgressBarMoving。

  1. - (void)makeMyProgressBarMoving {  
  2.  
  3.     float actual = [threadProgressView progress];  
  4.     threadValueLabel.text = [NSString stringWithFormat:@"%.2f", actual];  
  5.     if (actual < 1) {  
  6.         threadProgressView.progress = actual + 0.01;  
  7.         [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];  
  8.     }  
  9.     else threadStartButton.hidden = NO;  

這里計算用于顯示的進度條的值,利用 NSTimer ,每0.5秒自增0.01,當(dāng)值等于1的時候,進度條為100%,退出函數(shù)并顯示剛才被隱藏的按鈕。

最后,添加 UISlider 的實現(xiàn)函數(shù),用來更改主線程中 Test Part 中的 label 值。

  1. - (IBAction) testValueSliderChanged:(UISlider *)sender {  
  2.     testValueLabel.text = [NSString stringWithFormat:@"%.2f", sender.value];  

編譯執(zhí)行,按下線程開始按鈕,你將看到進度條的計算是在后臺運行,如圖所示:

iPhone開發(fā)多線程使用與注意事項

使用線程的注意事項

線程的堆棧大小

iPhone設(shè)備上的應(yīng)用程序開發(fā)也是屬于嵌入式設(shè)備的開發(fā),同樣需要注意嵌入式設(shè)備開發(fā)時的幾點問題,比如資源上限,處理器速度等。

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

你可以用下面的例子測試你的設(shè)備,這里使用POSIX Thread(pthread),設(shè)備環(huán)境是 iPhone 3GS(16GB)、SDK是3.1.3。

  1. #include "pthread.h"  
  2.  
  3. void *threadFunc(void *arg) {  
  4.     void*  stack_base = pthread_get_stackaddr_np(pthread_self());  
  5.     size_t stack_size = pthread_get_stacksize_np(pthread_self());  
  6.     NSLog(@"Thread: base:%p / size:%u", stack_base, stack_size);  
  7.     return NULL;  
  8. }  
  9.  
  10. - (void)applicationDidFinishLaunching:(UIApplication *)application {  
  11.     void*  stack_base = pthread_get_stackaddr_np(pthread_self());  
  12.     size_t stack_size = pthread_get_stacksize_np(pthread_self());  
  13.     struct rlimit limit;  
  14.     getrlimit(RLIMIT_STACK, &limit);  
  15.     NSLog(@"Main thread: base:%p / size:%u", stack_base, stack_size);  
  16.     NSLog(@"  rlimit-> soft:%llu / hard:%llu", limit.rlim_cur, limit.rlim_max);  
  17.  
  18.     pthread_t thread;  
  19.     pthread_create(&thread, NULL, threadFunc, NULL);  
  20.  
  21.     // Override point for customization after app launch  
  22.     [window addSubview:viewController.view];  
  23.     [window makeKeyAndVisible];  

結(jié)果如下:

模擬器

  1. Main thread: base:0xc0000000 / size:524288  
  2. rlimit-> soft:8388608 / hard:67104768  
  3. Thread: base:0xb014b000 / size:524288 

設(shè)備

  1. Main thread: base:0x30000000 / size:524288  
  2. rlimit-> soft:1044480 / hard:1044480  
  3. Thread: base:0xf1000 / size:524288 

由此可見,當(dāng)你測試多線程的程序時,模擬器和實際設(shè)備的堆棧大小是不一樣的。如果有大量遞歸函數(shù)調(diào)用可要注意了。

Autorelease

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

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

一般,在線程中使用內(nèi)存的模式是,線程最初

  1. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 

而在線程結(jié)束的時候

  1. [pool drain] 或 [pool release]。1 

子線程中描畫窗口

多線程編程中普遍遵循一個原則,就是一切與UI相關(guān)的操作都有主線程做,子線程只負(fù)責(zé)事務(wù),數(shù)據(jù)方面的處理。那么如果想在子線程中更新UI時怎么做呢?如果是在windows下,你會 PostMessage 一個描畫更新的消息,在iPhone中,需要使用performSelectorOnMainThread 委托主線程處理。

比如,如果在子線程中想讓 UIImageView 的 image 更新,如果直接在線程

  1. imageView.image = [UIImage imageNamed:@"Hoge.png"]; 

這么做,什么也不會出現(xiàn)的。需要將該處理委托給主線程來做,像下面:

  1. [delegate performSelectorOnMainThread:@selector(theProcess:) withObject:nil waitUntilDone:YES]; 

小結(jié):iPhone開發(fā)多線程使用與注意事項的內(nèi)容介紹完了,希望本文對你有所幫助!

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

2011-07-21 15:20:31

iPhone SDK 多線程

2011-07-25 17:48:10

iPhone 內(nèi)存

2011-07-21 15:40:24

iPhone 內(nèi)存管理 對象

2011-07-06 11:13:29

iOS游戲開發(fā)

2011-06-14 15:25:28

C++多線程

2012-03-11 18:46:18

iPhone4S

2025-04-03 07:33:56

2009-08-27 10:40:56

Java路徑

2013-09-25 10:15:51

閃存存儲優(yōu)勢注意事項

2011-08-08 13:50:29

iPhone開發(fā) NSOperatio 多線程

2009-12-15 10:10:38

VS 2008開發(fā)

2009-12-16 16:02:30

Visual Stud

2010-07-26 10:59:59

SQL Server游

2010-11-26 16:27:01

MySQL使用變量

2011-07-19 10:16:58

噴墨打印機注意事項

2010-01-18 14:25:19

使用C++Builde

2011-07-22 17:35:17

java路徑

2010-05-11 11:03:41

Mysql索引

2010-05-31 09:58:48

MySQL備份

2011-04-14 11:28:07

光纖
點贊
收藏

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