1.创建线程的三种方式 (队列线程)
1.1block创建线程
NSBlockOperation *operation = [NSBlockOperationblockOperationWithBlock:^{
for (int i =0; i < 10; i ++) {
NSLog(@"thread = = %@",[NSThreadcurrentThread]);
}
}];
//开启线程
[operation start];
1.2用类方法创建线程(包含两种方式+对象创建) 类方法创建不用 打开 因为系统为我们自动打开
1.21 对象方法
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"线程A"];
[thread start];
1.22 类
[NSThreaddetachNewThreadSelector:@selector(run:)toTarget:selfwithObject:@"线程B"];
1.23 类
[selfperformSelectorInBackground:@selector(downLoadImage)withObject:nil];
1.3用队列
NSOperationQueue *queue = [[NSOperationQueuealloc]init];
for (int i = 0; i < 10 ; i ++) {
NSInvocationOperation *operation = [[NSInvocationOperationalloc]initWithTarget:self selector:@selector(run:)object:@"队列线程"];
[queue addOperation:operation];
}
2.网络加载的线程
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return100;
}
//代理方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
staticNSString *string = @"cell";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:string];
if (cell ==nil) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:string];
}
_image = cell.imageView;
cell.imageView.image = [UIImageimageNamed:@"100.jpg"];
[selfloadImageView:_image];
return cell;
}
-(void)loadImageView:(UIImageView *)imV{
[NSThreaddetachNewThreadSelector:@selector(run:)toTarget:selfwithObject:imV];
}
-(void)run:(UIImageView *)imgV{
NSLog(@"data == %@",[NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://img4.duitang.com/uploads/blog/201402/07/20140207151136_JkBcf.jpeg"]]);
//UIImage *image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201402/07/20140207151136_JkBcf.jpeg"]]];
UIImage *image = [UIImageimageWithData:[NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://img4.duitang.com/uploads/blog/201402/07/20140207151136_JkBcf.jpeg"]]];
[imgV performSelectorOnMainThread:@selector(setImage:)withObject:image waitUntilDone:NO];
}
-(void)updateImage:(UIImage *)ima{
_image.image = ima;
}
3.火车票 线程保护
- (IBAction)open:(UIButton *)sender {
//创建三个窗口卖火车票
NSThread * thread1 = [[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
thread1.name = @"1";
NSLog(@"thread == %@",thread1);
NSThread * thread2 = [[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
thread2.name = @"2";
NSThread * thread3 = [[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
thread3.name = @"3";
[thread1 start];
[thread2 start];
[thread3 start];
}
-(void)run {
while (true) {
/* if (_count > 0) {
[NSThread sleepForTimeInterval:0.02];
_count--;
NSLog(@"卖票窗口:: %@ ,已经卖了一张票,剩余票数::%d",[NSThread currentThread],_count);
}else{
NSLog(@"thread == %@",[NSThread currentThread]);
[NSThread exit];
}*/
//卖火车票问题 不允许其他线程访问锁住
@synchronized(self) {
if (_count > 0) {
[NSThread sleepForTimeInterval:0.02];
_count--;
NSLog(@"卖票窗口:: %@ ,已经卖了一张票,剩余票数::%d",[NSThread currentThread],_count);
}else{
NSLog(@"thread == %@",[NSThread currentThread]);
[NSThread exit];
}
}
}
}
4.网络加载多线程
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *string = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
}
_image = cell.imageView;
cell.imageView.image = [UIImage imageNamed:@"100.jpg"];
[self loadImageView:_image];
return cell;
}
-(void)loadImageView:(UIImageView *)imV{
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:imV];
}
-(void)run:(UIImageView *)imgV{
NSLog(@"data == %@",[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201402/07/20140207151136_JkBcf.jpeg"]]);
//UIImage *image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201402/07/20140207151136_JkBcf.jpeg"]]];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201402/07/20140207151136_JkBcf.jpeg"]]];
[imgV performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
}