午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    iphone中如何進(jìn)行多線程編程
    來(lái)源:易賢網(wǎng) 閱讀:881 次 日期:2014-11-04 10:54:16
    溫馨提示:易賢網(wǎng)小編為您整理了“iphone中如何進(jìn)行多線程編程”,方便廣大網(wǎng)友查閱!

    名單

    多線程在各種編程語(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)建主要有二種方式:

    - (id)init; // designated initializer

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

    + (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument前兩種方法創(chuàng)建后,需要手機(jī)啟動(dòng),啟動(dòng)的方法是:

    - (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è)例子:

    SellTicketsAppDelegate.h 文件

    // SellTicketsAppDelegate.h

    import

    @interface SellTicketsAppDelegate : NSObject {

    int tickets;

    int count;

    NSThread* ticketsThreadone;

    NSThread* ticketsThreadtwo;

    NSCondition* ticketsCondition;

    UIWindow *window;

    }

    @property (nonatomic, retain) IBOutlet UIWindow *window;

    @end

    SellTicketsAppDelegate.m 文件

    // SellTicketsAppDelegate.m

    import "SellTicketsAppDelegate.h"

    @implementation SellTicketsAppDelegate

    @synthesize window;

    - (void)applicationDidFinishLaunching:(UIApplication *)application {

     

    tickets = 100;

    count = 0;

    // 鎖對(duì)象

    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āng)前票數(shù)是:%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

    三、線程的交互

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

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

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

    - (void)startTheBackgroundJob {

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

    // to do something in your thread job

    ...

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

    [pool release];

    }

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

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

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

    更多信息請(qǐng)查看IT技術(shù)專欄

    更多信息請(qǐng)查看技術(shù)文章
    易賢網(wǎng)手機(jī)網(wǎng)站地址:iphone中如何進(jìn)行多線程編程
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國(guó)考·省考課程試聽報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)