首页 > 系统 > iOS > 正文

实例解析iOS应用多线程开发中NSthread类的用法

2019-10-21 18:56:00
字体:
来源:转载
供稿:网友
这篇文章主要介绍了iOS应用多线程开发中NSthread类的用法,代码基于传统的Objective-C,NSthread类需要的朋友可以参考下
 

一、NSthread的初始化
1.动态方法

复制代码代码如下:

- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;  
// 初始化线程  
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];  
// 设置线程的优先级(0.0 - 1.0,1.0最高级)  
thread.threadPriority = 1;  
// 开启线程  
[thread start];
  
参数解析:
selector :线程执行的方法,这个selector最多只能接收一个参数
target :selector消息发送的对象
argument : 传给selector的唯一参数,也可以是nil

 

2.静态方法

复制代码代码如下:

+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument;  
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];  
// 调用完毕后,会马上创建并开启新线程  

 

3.隐式创建线程的方法

复制代码代码如下:

[self performSelectorInBackground:@selector(run) withObject:nil];  

 

二、获取当前线程

复制代码代码如下:

NSThread *current = [NSThread currentThread];
 

 

三、获取主线程

复制代码代码如下:

NSThread *main = [NSThread mainThread]; 

 

四、暂停当前线程

复制代码代码如下:

// 暂停2s  
[NSThread sleepForTimeInterval:2];    
// 或者  
NSDate *date = [NSDate dateWithTimeInterval:2 sinceDate:[NSDate date]];  
[NSThread sleepUntilDate:date];  

 

五、线程间的通信
1.在指定线程上执行操作

复制代码代码如下:

[self performSelector:@selector(run) onThread:thread withObject:nil waitUntilDone:YES];  

2.在主线程上执行操作
复制代码代码如下:

[self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];  

3.在当前线程执行操作
复制代码代码如下:

[self performSelector:@selector(run) withObject:nil];  

 

六、优缺点
1.优点:NSThread比其他两种多线程方案较轻量级,更直观地控制线程对象
2.缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销

七、下载图片的例子:
新建singeView app
新建项目,并在xib文件上放置一个imageView控件。按住control键拖到viewControll
er.h文件中创建imageView IBOutlet 
ViewController.m中实现:

复制代码代码如下:

//  
//  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的方法,除了可以更新主线程的数据外,还可以更新其他线程的比如:
用:
复制代码代码如下:
performSelector:onThread:withObject:waitUntilDone:

运行下载图片:

 

实例解析iOS应用多线程开发中NSthread类的用法

图片下载下来了。



注:相关教程知识阅读请移步到IOS开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表