NSThread的基本使用

使用NSThread创建线程的两种方式一个NSThread对象对应一条线程
第一种方式:使用alloc init 创建线程,必须手动启动线程
优点:可对线程进行更详细的设置,如下述设置线程名称和优先级

- (void)thread
{
    //创建线程
    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(threadSelector) object:nil];
    //设置线程名称
    [thread setName:@"a"];
    //线程优先级  取值范围 0.1 ~ 1.0之间  默认优先级是0.5
    thread.threadPriority = 1;
    //启动线程
    [thread start];
    
    //创建线程
    NSThread *thread1 = [[NSThread alloc]initWithTarget:self selector:@selector(threadSelector) object:nil];
    [thread1 setName:@"b"];
    //启动线程
    [thread1 start];
}

- (void)threadSelector{
    NSLog(@"%@",[NSThread currentThread]);
}

特别说明:
以下述方式启动线程,线程方法是在内部 - (void)main;函数中调用
大家可继承NSThread,尝试在main函数中输出结果

NSThread *thread = [[NSThread alloc]init];
//启动线程
[thread start];

第二种方式:分离子线程,会自动启动线程
优点:简单快捷
缺点:无法对线程进行更详细的设置

- (void)thread{
    //创建线程
    [NSThread detachNewThreadSelector:@selector(threadSelector) toTarget:self withObject:nil];
}

- (void)threadSelector{
    NSLog(@"%@",[NSThread currentThread]);
}
@end

开启一条后台线程

- (void)thread3{
    
    [self performSelectorInBackground:@selector(threadSelector) withObject:nil];
}

什么叫做线程间通信?

在一个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信

线程间通信的体现

1个线程传递数据给另一个线程
在1个线程中执行完特定任务后,转到另1个线程继续执行任务

线程间通信常用方法

-(void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
-(void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;

代码

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end

@implementation ViewController

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [NSThread detachNewThreadSelector:@selector(download) toTarget:self withObject:nil];
//    [self download1];
}

- (void)download{
    
    NSURL *url = [NSURL URLWithString:@"http://pic.sc.chinaz.com/files/pic/pic9/201508/apic14052.jpg"];
    
    NSData *imgData = [NSData dataWithContentsOfURL:url];
    
    UIImage *image = [UIImage imageWithData:imgData];
    
    //回到主线程
//    [self performSelectorOnMainThread:@selector(modifyUI:) withObject:image waitUntilDone:NO];
    [self performSelector:@selector(modifyUI:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO];
    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
    
}

- (void)modifyUI:(UIImage *)image{
    self.imageView.image = image;
}
//计算下载图片的时间:老土
- (void)download1
{
    NSDate *start = [NSDate date];//获得当前的时间
    NSURL *url = [NSURL URLWithString:@"http://pic.sc.chinaz.com/files/pic/pic9/201508/apic14052.jpg"];
    
    NSData *imgData = [NSData dataWithContentsOfURL:url];
    
    UIImage *image = [UIImage imageWithData:imgData];
    
    self.imageView.image = image;
    NSDate *end = [NSDate date];//获得当前的时间
    
    NSLog(@"%f",[end timeIntervalSinceDate:start]);
}
//计算下载图片的时间
- (void)download2
{
    CFTimeInterval start = CFAbsoluteTimeGetCurrent();
    
 
    NSURL *url = [NSURL URLWithString:@"http://pic.sc.chinaz.com/files/pic/pic9/201508/apic14052.jpg"];
    
    NSData *imgData = [NSData dataWithContentsOfURL:url];
    
    CFTimeInterval end = CFAbsoluteTimeGetCurrent();
    
    NSLog(@"%f",end -start);
    
    UIImage *image = [UIImage imageWithData:imgData];
    
    self.imageView.image = image;

}

@end

你可能感兴趣的:(NSThread的基本使用)