相關(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)品