1 //方式一:利用perform開啓多線程,而且執行方法threadAction 2 // [self performSelectorInBackground:@selector(threadAction) withObject:@"thread"]; 3 4 //方式二: 5 // NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadAction:) object:@"text"]; 6 // thread.name = @"thread1"; 7 // //開啓線程 8 // [thread start]; 9 10 //方式三:開啓新的線程,而且執行 11 [NSThread detachNewThreadSelector:@selector(threadAction:) toTarget:self withObject:@"thread2"];
1 for (int i=0; i<50; i++) { 2 NSLog(@"主線程:%d",i); 3 }
在多線程的threadAction:方法裏也一樣寫一個for循環數組
1 for (int i=0; i<50; i++) { 2 NSLog(@"多線程:%d",i); 3 }
1 //獲取當前線程 2 NSThread *thread = [NSThread currentThread]; 3 //判斷當前是否在多線程 4 [NSThread isMultiThreaded] 5 //判斷當前是否在主線程 6 [NSThread isMainThread] 7 //讓當前線程睡眠幾秒 8 [NSThread sleepForTimeInterval:3]; 9 //回到主線程 10 // [self performSelectorOnMainThread:<#(SEL)#> withObject:<#(id)#> waitUntilDone:<#(BOOL)#>]
1 #import <UIKit/UIKit.h> 2 3 @interface UIImageView (cache) 4 //爲UIImageView寫一個添加網絡圖片的方法 5 - (void)setimage:(NSString *)str; 6 7 @end
類目的.m文件 #import "UIImageView+cache.h" @implementation UIImageView (cache) //若是這樣直接寫方法,在主線程,當咱們在加載網絡時不能滑動TextView - (void)setimage:(NSString *)str { NSURL *url = [NSURL URLWithString:str]; NSData *data = [NSData dataWithContentsOfURL:url]; self.image = [UIImage imageWithData:data]; } @end
類目寫好了,讓咱們用起來吧。 viewController裏的代碼以下: #import "ViewController.h" #import "UIImageView+cache.h" @interface ViewController () { UIImageView *_image; NSMutableArray *arr; } - (IBAction)click:(UIButton *)sender; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; arr = [NSMutableArray array]; //建立八行六列的UIImageView for (int i = 0; i < 6; i ++) { for (int j = 0 ; j < 8; j ++) { _image = [[UIImageView alloc]initWithFrame:CGRectMake(i * 62, j * 62 , 60, 60)]; _image.backgroundColor = [UIColor yellowColor]; [self.view addSubview:_image]; //將建立好的UIImageView放進可變數組。 [arr addObject:_image]; } } } - (IBAction)click:(UIButton *)sender { for (UIImageView *imageview in arr) { //利用分類給數組裏的UIImageView添加圖片 [imageview setimage:@"http://img31.mtime.cn/pi/2013/03/08/144644.81111130_1280X720.jpg"]; } } @end
那麼爲了解決這樣阻塞主線程的狀況 咱們把分類的方法該爲: - (void)setimage:(NSString *)str { //開啓一個多線程,而且把str經過建立多線程傳遞到多線程的任務中(注:這裏的字符串爲網絡圖片的地址) [NSThread detachNewThreadSelector:@selector(thredAction:) toTarget:self withObject:str]; } //多線程的任務 - (void)thredAction:(NSString *)str { //將字符串轉換成URL NSURL *url = [NSURL URLWithString:str]; //將url轉化成data NSData *data = [NSData dataWithContentsOfURL:url]; //注意:UI的修改只能放在主線程 因此寫在這裏仍是錯誤 //self.image = [UIImage imageWithData:data]; //回到主線程 [self performSelectorOnMainThread:@selector(setImage:) withObject:[UIImage imageWithData:data] waitUntilDone:YES]; } 這樣就能夠解決線程阻塞的問題了。