mac下多線程實現處理

來源:互聯網
上載者:User

mac下線程開啟
注意:
1、新的線程必須考慮設立一個autorelease池處理自動釋放的代碼。
模版如下:
NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ]; 
[code here] 
[pool release]; 
如果你能確保自己寫的代碼自己申請,自己釋放的話,不使用autorelease的對象,那麼建議:採用自己釋放的方式
2、
下來是Run Loop的使用場合:
1. 使用port或是自訂的input source來和其他線程進行通訊
2. 線上程(非主線程)中使用timer
3. 使用 performSelector...系列(如performSelectorOnThread, ...)
4. 使用線程執行循環性工作
3、說哈線程和NSTimer
http://3426724.blog.51cto.com/3416724/747650
參考這地址,進行實踐
線程執行實現:
- (void)thread3
{
    NSLog(@"t3 = %@ ,isMainThread = %d ,isMulti = %d" ,[NSThread currentThread] ,[NSThread isMainThread] ,[NSThread
isMultiThreaded]);
    
    while (YES)
    {        
        [NSThread sleepForTimeInterval:3];
        
        break;
    }
    
    self->btnBind.hidden = NO;    
    
    return;
}
手工啟動線程:
- (IBAction)bindEmail:(id)sender
{
    [self Test039];
    
    NSLog(@"over");
}
線程啟動如下:
- (void)Test038
{
    /// 方法2 能啟動,timer事件正常完成下timer退出,timer啟動線程能釋放,thread3作為非主線程函數執行
    NSAutoreleasePool *timerNPool = [[NSAutoreleasePool alloc] init];
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(thread3) userInfo:nil repeats:NO];
    [runloop run];
    [timerNPool release];
    
    /// 方法3,結論:啟動thread3,作為主線程的一個函數執行
//    NSTimer *timer1 = [NSTimer timerWithTimeInterval:15 target:self selector:@selector(thread3) userInfo:nil
repeats:NO];
//    [[NSRunLoop mainRunLoop] addTimer:timer1 forMode:NSDefaultRunLoopMode];
}
- (void)Test039
{
    // 啟動一個線程,線程對應使用timer(未使用方法2和方法3,直接timer)。結論:無法啟動timer
    //[NSThread detachNewThreadSelector:@selector(Test038) toTarget:self withObject:nil];
    [NSThreadManager StartNewThreadWithTarget:self selector:@selector(Test038) object:nil];

}

轉自:http://www.cnblogs.com/GoGoagg/archive/2012/03/31/2426475.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.