iOS多線程之NSThread

相關(guān)文章:
iOS多線程之GCD
iOS多線程之NSOperations

案例1--圖片下載

#define kImageUrl   @"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 首先啟動(dòng)一個(gè)線程去下載圖片
    // 方式1
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(downloadImage:) object:kImageUrl];
    [thread start];
    // 方式2,會(huì)隱式的創(chuàng)建一個(gè)NSThread
    // [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kImageUrl];
}
// 下載圖片
- (void)downloadImage:(NSString *)imageUrl
{
    NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageUrl]];
    if(data)
    {
        UIImage *image = [[UIImage alloc] initWithData:data];
        if(image)
        {
            [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
        }
    }
}
// 在主線程刷新UI
- (void)updateUI:(UIImage *)image
{
    imgView.image = image;
}
@end

案例2--多售票窗口同時(shí)售票

說明:票源是共享數(shù)據(jù),每個(gè)售票窗口相當(dāng)于一個(gè)線程,為了保證數(shù)據(jù)的一致性,需要在每次售票時(shí)對(duì)票源加鎖,票售出后釋放鎖

@interface ViewController ()
{
    NSThread *thread1;
    NSThread *thread2;
    NSLock *lock;
    NSInteger ticket;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    lock = [[NSLock alloc] init];
    ticket = 20; //總共20張票
    thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTickets) object:nil];
    [thread1 setName:@"窗口1"];
    thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTickets) object:nil];
    [thread2 setName:@"窗口2"];
    [thread1 start];
    [thread2 start];
}
- (void)saleTickets
{
    while (TRUE)
    {
        //售票前加鎖
        [lock lock];
        if(ticket <= 0)
        {
            [lock unlock];
            NSLog(@"沒有票源了...");
            break;
        }
        ticket--;
        NSLog(@"%@ 當(dāng)前余票:%zd,系統(tǒng)已售出:%zd", [[NSThread currentThread] name], ticket, (20 - ticket));
        //票售出后釋放鎖
        [lock unlock];
        //每售出一張票后,售票員需要休息一下下
        [NSThread sleepForTimeInterval:arc4random() % 3];
    }
}
@end

輸出:(實(shí)際輸出結(jié)果可能每次不一樣,因?yàn)槭燮眴T休息時(shí)間隨機(jī))

窗口1 當(dāng)前余票:19,系統(tǒng)已售出:1
窗口2 當(dāng)前余票:18,系統(tǒng)已售出:2
窗口2 當(dāng)前余票:17,系統(tǒng)已售出:3
窗口1 當(dāng)前余票:16,系統(tǒng)已售出:4
窗口2 當(dāng)前余票:15,系統(tǒng)已售出:5
窗口1 當(dāng)前余票:14,系統(tǒng)已售出:6
窗口2 當(dāng)前余票:13,系統(tǒng)已售出:7
窗口1 當(dāng)前余票:12,系統(tǒng)已售出:8
窗口1 當(dāng)前余票:11,系統(tǒng)已售出:9
窗口2 當(dāng)前余票:10,系統(tǒng)已售出:10
窗口1 當(dāng)前余票:9,系統(tǒng)已售出:11
窗口2 當(dāng)前余票:8,系統(tǒng)已售出:12
窗口2 當(dāng)前余票:7,系統(tǒng)已售出:13
窗口2 當(dāng)前余票:6,系統(tǒng)已售出:14
窗口2 當(dāng)前余票:5,系統(tǒng)已售出:15
窗口1 當(dāng)前余票:4,系統(tǒng)已售出:16
窗口1 當(dāng)前余票:3,系統(tǒng)已售出:17
窗口1 當(dāng)前余票:2,系統(tǒng)已售出:18
窗口1 當(dāng)前余票:1,系統(tǒng)已售出:19
窗口2 當(dāng)前余票:0,系統(tǒng)已售出:20
窗口1 沒有票源了...
窗口2 沒有票源了...


案例3--生產(chǎn)者、消費(fèi)者

說明:生來看看NSConditionLock的定義

@interface NSConditionLock : NSObject <NSLocking> 
{
// 初始化with condition
- (instancetype)initWithCondition:(NSInteger)condition NS_DESIGNATED_INITIALIZER;
// 注意是只讀的
@property (readonly) NSInteger condition;
// 如果滿足條件(*成員變量condition*==*參數(shù)變量condition*)則成功獲取鎖
- (void)lockWhenCondition:(NSInteger)condition;
// 釋放鎖,并且讓**設(shè)置新的condition值**
- (void)unlockWithCondition:(NSInteger)condition;
@end

樣例代碼:

@interface ViewController ()
{
    NSThread *producerThread;
    NSThread *consumerThread;
    NSConditionLock *plock;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    producerThread = [[NSThread alloc] initWithTarget:self selector:@selector(producerRun) object:nil];
    consumerThread = [[NSThread alloc] initWithTarget:self selector:@selector(consumerRun) object:nil];
    plock = [[NSConditionLock alloc] initWithCondition:0];
    
    [producerThread start];
    [consumerThread start];
}

- (void)producerRun
{
    while (TRUE)
    {
        [plock lockWhenCondition:0];
        [NSThread sleepForTimeInterval:1];
        NSLog(@"制造了一個(gè)產(chǎn)品");
        [plock unlockWithCondition:1];
    }
}

- (void)consumerRun
{
    while(TRUE)
    {
        [plock lockWhenCondition:1];
        [NSThread sleepForTimeInterval:1];
        NSLog(@"消費(fèi)了一個(gè)產(chǎn)品");
        [plock unlockWithCondition:0];
    }
}
@end

輸出:

制造了一個(gè)產(chǎn)品
消費(fèi)了一個(gè)產(chǎn)品
制造了一個(gè)產(chǎn)品
消費(fèi)了一個(gè)產(chǎn)品
制造了一個(gè)產(chǎn)品
消費(fèi)了一個(gè)產(chǎn)品

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、簡(jiǎn)介:1.1 iOS有三種多線程編程的技術(shù),分別是:1.、NSThread2、Cocoa NSOperatio...
    LuckTime閱讀 1,447評(píng)論 0 1
  • NSThread: NSThread 是一個(gè)控制線程執(zhí)行的對(duì)象,它不如 NSOperation抽象,通過它我們可以...
    甘哲157閱讀 666評(píng)論 0 4
  • 進(jìn)程 是程序運(yùn)行的實(shí)例,是系統(tǒng)進(jìn)行資源分配和調(diào)度的一個(gè)獨(dú)立單位,它包括獨(dú)立的地址空間,資源以及1個(gè)或多個(gè)線程。比如...
    ptlCoder閱讀 431評(píng)論 0 0
  • 基本概念 進(jìn)程: 一個(gè)具有一定獨(dú)立功能的程序關(guān)于某個(gè)數(shù)據(jù)集合的一次運(yùn)行活動(dòng)??梢岳斫獬梢粋€(gè)運(yùn)行中的應(yīng)用程序。線程:...
    charlotte2018閱讀 562評(píng)論 0 4
  • 一、NSThread 1.創(chuàng)建和啟動(dòng)線程的3種方式 1>先創(chuàng)建,后啟動(dòng) //創(chuàng)建 NSThread *thread...
    記憶是座荒島001閱讀 521評(píng)論 0 0

友情鏈接更多精彩內(nèi)容