声明
int tickets;
int count;
NSThread* ticketsThreadone;
NSThread* ticketsThreadtwo;
NSCondition* ticketsCondition;
NSLock *theLock;
实现 创建线程
tickets = 100;
count = 0;
theLock = [[NSLock alloc] init];
// 锁对象
ticketsCondition = [[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 *ticketsThreadthree = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[ticketsThreadthree setName:@"Thread-3"];
[ticketsThreadthree start];
run方法
- (void)run{
while (TRUE) {
// // 上锁
// [ticketsCondition lock];
// [theLock lock];//锁住
// if(tickets >= 0){
// [NSThread sleepForTimeInterval:0.09];
// count = 100 - tickets;
// NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThread currentThread] name]);
// tickets--;
// }else{
// break;
// }
// [theLock unlock];//开锁
// [ticketsCondition unlock];
//线程锁 (简化)
@synchronized(self) {
if(tickets >= 0){
[NSThread sleepForTimeInterval:0.09];
count = 100 - tickets;
NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThread currentThread] name]);
tickets--;
}else{
break;
}
}//end @synchronized
}//end while
}
例子:线程下载图片
//
// ViewController.m
// NSThreadDemo
//
// Created by 屎壳郎情调 on 13-9-23.
// Copyright (c) 2013年 ibokan. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
/*线程间通讯
线程下载完图片后怎么通知主线程更新界面呢?
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
performSelectorOnMainThread是NSObject的方法,除了可以更新主线程的数据外,还可以更新其他线程的比如:
用:performSelector:onThread:withObject:waitUntilDone:
*/
- (void)viewDidLoad
{
[super viewDidLoad];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 210, 320, 300)];
[self.view addSubview:imageView];
[imageView release];
[self.view addSubview:imageView1];
[imageView1 release];
[NSThread detachNewThreadSelector:@selector(downImage1) toTarget:self withObject:nil];
//线程
[NSThread detachNewThreadSelector:@selector(downImage) toTarget:self withObject:nil];
//(等效)
//NSThread *th = [[NSThread alloc] initWithTarget:self selector:@selector(downImage) object:nil];
//[th start];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//下载图片
-(void)downImage
{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://aa540933120.host.yzdns.cn/images/baidu.jpg"]];
UIImage *image = [UIImage imageWithData:data];
if(image!=nil)
{
//跟新主线程(线程的通信)
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
}
}
//下载图片
-(void)downImage1
{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.5wants.cc/WEB/File/U3325P704T93D1661F3923DT20090612155225.jpg"]];
UIImage *image = [UIImage imageWithData:data];
if(image!=nil)
{
//跟新主线程(线程的通信)
[self performSelectorOnMainThread:@selector(updateUI1:) withObject:image waitUntilDone:YES];
}
}
//刷新页面UI
-(void)updateUI:(UIImage *)image
{
imageView.image = image;
}
//刷新页面UI
-(void)updateUI1:(UIImage *)image
{
imageView1.image = image;
}
@end