實例解析iOS應(yīng)用多線程開發(fā)中NSthread類的用法
一、NSthread的初始化
1.動態(tài)方法
- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;
// 初始化線程
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
// 設(shè)置線程的優(yōu)先級(0.0 - 1.0,1.0最高級)
thread.threadPriority = 1;
// 開啟線程
[thread start];
參數(shù)解析:
selector :線程執(zhí)行的方法,這個selector最多只能接收一個參數(shù)
target :selector消息發(fā)送的對象
argument : 傳給selector的唯一參數(shù),也可以是nil
2.靜態(tài)方法
+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument;
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
// 調(diào)用完畢后,會馬上創(chuàng)建并開啟新線程
3.隱式創(chuàng)建線程的方法
[self performSelectorInBackground:@selector(run) withObject:nil];
二、獲取當(dāng)前線程
NSThread *current = [NSThread currentThread];
三、獲取主線程
NSThread *main = [NSThread mainThread];
四、暫停當(dāng)前線程
// 暫停2s
[NSThread sleepForTimeInterval:2];
// 或者
NSDate *date = [NSDate dateWithTimeInterval:2 sinceDate:[NSDate date]];
[NSThread sleepUntilDate:date];
五、線程間的通信
1.在指定線程上執(zhí)行操作
[self performSelector:@selector(run) onThread:thread withObject:nil waitUntilDone:YES];
2.在主線程上執(zhí)行操作
[self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];
3.在當(dāng)前線程執(zhí)行操作
[self performSelector:@selector(run) withObject:nil];
六、優(yōu)缺點
1.優(yōu)點:NSThread比其他兩種多線程方案較輕量級,更直觀地控制線程對象
2.缺點:需要自己管理線程的生命周期,線程同步。線程同步對數(shù)據(jù)的加鎖會有一定的系統(tǒng)開銷
七、下載圖片的例子:
新建singeView app
新建項目,并在xib文件上放置一個imageView控件。按住control鍵拖到viewControll
er.h文件中創(chuàng)建imageView IBOutlet
ViewController.m中實現(xiàn):
//
// ViewController.m
// NSThreadDemo
//
// Created by rongfzh on 12-9-23.
// Copyright (c) 2012年 rongfzh. All rights reserved.
//
#import "ViewController.h"
#define kURL @"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"
@interface ViewController ()
@end
@implementation ViewController
-(void)downloadImage:(NSString *) url{
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [[UIImage alloc]initWithData:data];
if(image == nil){
}else{
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
}
}
-(void)updateUI:(UIImage*) image{
self.imageView.image = image;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImage:) object:kURL];
[thread start];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
線程間通訊
線程下載完圖片后怎么通知主線程更新界面呢?
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
performSelectorOnMainThread是NSObject的方法,除了可以更新主線程的數(shù)據(jù)外,還可以更新其他線程的比如:
用:
運行下載圖片:

圖片下載下來了。
相關(guān)文章
IOS使用UICollectionView實現(xiàn)無限輪播效果
這篇文章主要為大家詳細介紹了IOS使用UICollectionView實現(xiàn)無限輪播效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03
iOS開發(fā)教程之識別圖片中二維碼功能的實現(xiàn)
長按識別二維碼這個功能相信對大家來說都不陌生,最近工作中就遇到了這個需求,所以下面這篇文章主要給大家介紹了關(guān)于利用iOS識別圖片中二維碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2018-07-07
iOS CoreMotion實現(xiàn)設(shè)備運動加速度計陀螺儀
這篇文章主要介紹了iOS CoreMotion實現(xiàn)設(shè)備運動加速度計陀螺儀,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
iOS應(yīng)用開發(fā)中導(dǎo)航欄按鈕UIBarButtonItem的添加教程
這篇文章主要介紹了iOS應(yīng)用開發(fā)中導(dǎo)航欄按鈕UIBarButtonItem的添加教程,文中詳細介紹了使用UINavigationController導(dǎo)航控制器添加的過程,需要的朋友可以參考下2016-02-02

