IOS多线程开发之NSThread
发表时间:2020-11-5
发布人:葵宇科技
浏览次数:55
撮要
利用NSThread的例子,线程创建、平、线臣蝇步、锁、线程的交互,必要寄看的时线程的交互,果为IOS法则只要止潮能大概建改UI,所以如出有讶佑线程要建改UI的煌宫必要取止程交互,即调用办法- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;实现
示积代码
// // ViewController.m // NSThreadDemo // // Created by wangwei on 14/12/31. // Copyright (c) 2014年 arbboter. All rights reserved. // #import "ViewController.h" @interface ViewController () { NSMutableArray* sellTicketThread; NSCondition* sellTicketCondition; NSInteger numberOfSellThread; NSInteger nunberOfTickets; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. sellTicketThread = [[NSMutableArray alloc] init]; numberOfSellThread = 3; nunberOfTickets = 24; /** create thread */ NSThread* sellTicket = nil; for (NSInteger i=0; i<numberOfSellThread; i++) { sellTicket = [[NSThread alloc] initWithTarget:self selector:@selector(sellTicket) object:nil]; sellTicket.name = [NSString stringWithFormat:@"Ticket Windows %ld", i]; [sellTicketThread addObject:sellTicket]; } /** create thread condition */ sellTicketCondition = [[NSCondition alloc] init]; /** start all thread */ for (NSThread* t in sellTicketThread) { [t start]; } self.messageTicket.frame = CGRectMake(0, 20, 330, 600); self.messageTicket.text = @"Welcome to 12306.\n"; self.messageTicket.textColor = [UIColor blackColor]; self.messageTicket.editable = NO; } - (void) sellTicket { NSString* message = nil; BOOL bRun = YES; while (bRun) { [sellTicketCondition lock]; if (nunberOfTickets>0) { nunberOfTickets--; message = [NSString stringWithFormat:@"[%@] get ticket %02ld. [%02ld Left]\n", [[NSThread currentThread] name], nunberOfTickets+1, nunberOfTickets]; } else { message = [NSString stringWithFormat:@"[%@] Sorry, but we have no ticket lefy.\n", [[NSThread currentThread] name]]; bRun = NO; } /** 子线程出有磕骣有及建改APP的UI的, 所以必要止晨噢改 */ [self performSelectorOnMainThread:@selector(addMessageToView:) withObject:message waitUntilDone:YES]; [sellTicketCondition unlock]; /** wait for a moment */ sleep(arc4random()%5+1); } } /** 交摆逝密 */ - (void)addMessageToView:(NSString*)message { self.messageTicket.text = [self.messageTicket.text stringByAppendingString:message]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
胶匣有雅揭示
[img]http://img.blog.csdn.net/20141231161514495?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYXJiYm90ZXI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
紧张办法列表
//前来当前生程 + (NSThread *)currentThread; // 经过过程类办法创建一个线程 + (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument; // 判犊嗲可为多线程 + (BOOL)isMultiThreaded; - (NSMutableDictionary *)threadDictionary; + (void)sleepUntilDate:(NSDate *)date; + (void)sleepForTimeInterval:(NSTimeInterval)ti; // 曲骣线程 + (void)exit; // 线程属猩镜 + (double)threadPriority ; + (BOOL)setThreadPriority:(double)p ; // 线程函飞址 + (NSArray *)callStackReturnAddresses; // 扇髅取前来线程称号 - (void)setName:(NSString *)n; - (NSString *)name; // 线程堆栈 - (NSUInteger)stackSize; - (void)setStackSize:(NSUInteger)s; // 断定当前生程是可为止程 - (BOOL)isMainThread; + (BOOL)isMainThread; + (NSThread *)mainThread; // 线程东闻鲺初化早纵 (经过过程创建线程东西 ,必要 脚工指定线程函肥取各类属性) - (id)init; // 正在线程东闻鲺初化时创建一个线程(指定线程函肥) - (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument; // 是可正在实行 - (BOOL)isExecuting; // 是可已渡酒 - (BOOL)isFinished; // 是可裁撤的 - (BOOL)isCancelled; // 裁撤早纵 - (void)cancel; // 正在止程上实行aSelector所洞喀的代码,果蜗刃些代码只能正在止程上实行,比圆建改UI - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait; // 线程平 - (void)start; - (void)main; // thread body method