NSThread的使用

NSThread每个NSThread对象对应一个线程,轻量级。
NSThread:优点:NSThread比其他俩个轻量级,使用简单。
                 缺点:需要自己管理线程的生命周期、线程同步、加锁、睡眠以及唤醒等。线程同步对数据的加锁会有一定的系统开销。

NSThread的几种创建方式
       //方式一:利用perform开启多线程,并且执行方法threadAction
//    [self performSelectorInBackground:@selector(threadAction) withObject:@"thread"];
    
    //方式二:
//    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadAction:) object:@"text"];
//    thread.name = @"thread1";
//    //开启线程
//    [thread start];
    
    //方式三:开启新的线程,并且执行
    [NSThread detachNewThreadSelector:@selector(threadAction:) toTarget:self withObject:@"thread2"];
    
对比主线程与多线程在执行上的先后顺序:
在viewDidLoad里写一个for循环。

    
for (int i=0; i<50; i++) {
        NSLog(@"主线程:%d",i);
    }


在多线程的 threadAction:方法里也同样写一个for循环
for (int i=0; i<50; i++) {
        NSLog(@"多线程:%d",i);
    }

打印结果:
通过俩次打印结果,我们知道他们是没有先后顺序的,而且每次打印都不同。
有几个常用的方法我们可能会用到:
//获取当前线程
NSThread *thread = [NSThread currentThread];
//判断当前是否在多线程
[NSThread isMultiThreaded]
//判断当前是否在主线程
[NSThread isMainThread]
//让当前线程睡眠几秒
[NSThread sleepForTimeInterval:3];
//回到主线程
//    [self performSelectorOnMainThread:<#(SEL)#> withObject:<#(id)#> waitUntilDone:<#(BOOL)#>]




思考:我们为什么要使用多线程?

总结: 提高CPU的利用率,让程序更加流畅。如果我们把所有的任务都放在主线程会造成主线程阻塞。比如说当你在加载较多的图片时,你的textView是不能滚动的。下面我们就针对这方面写一个demo.

首先我们给UIimageView添加一个类目,用来让其能够加载网络图片
类目的.h文件
#import <UIKit/UIKit.h>

@interface UIImageView (cache)
//为UIImageView写一个添加网络图片的方法
- (void)setimage:(NSString *)str;

@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];
    
}
这样就可以解决线程阻塞的问题了。







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值