UI - NSThread

UI - NSThread_第1张图片

<ViewController.h>

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

<ViewController.m>

#import "ViewController.h"

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}

- (IBAction)button1Action:(id)sender {
    
 #pragma  mark -- NSThread
    //a.该方法手动创建线程,必须手动开启.好处在于可以设置 NSThread 中的一些属性
//    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(print) object:nil];
//    [thread start];
//    [thread release];
    
    //b.该方法直接指派一个线程执行一个任务,并且直接执行线程内容,不需要手动开启.而且没有返回值
//    [NSThread detachNewThreadSelector:@selector(print) toTarget:self withObject:nil];
    
 #pragma  mark -- 利用 NSObject 的分类来执行

//    [self performSelector:@selector(print) withObject:nil];
//    使用performselector: 和直接调用的区别
    /*
     下面两段代码都在主线程中运行,我们在看别人代码时会发现有时会直接调用,有时会利用performSelector调用,今天看到有人在问这个问题,我便做一下总结,
     [delegate imageDownloader:self didFinishWithImage:image];
     [delegate performSelector:@selector(imageDownloader:didFinishWithImage:)withObject:self withObject:image];
     
     1、performSelector是运行时系统负责去找方法的,在编译时候不做任何校验;如果直接调用编译时会自动校验。如果imageDownloader:didFinishWithImage:image:不存在,那么直接调用 在编译时候就能够发现(借助Xcode可以写完就发现),但是使用performSelector的话一定是在运行时候才能发现(此时程序崩溃);Cocoa支持在运行时向某个类添加方法,即方法编译时不存在,但是运行时候存在,这时候必然需要使用performSelector去调用。所以有时候如果使用了performSelector,为了程序的健壮性,会使用检查方法 - (BOOL)respondsToSelector:(SEL)aSelector;
     2、直接调用方法时候,一定要在头文件中声明该方法的使用,也要将头文件import进来。而使用performSelector时候, 可以不用import头文件包含方法的对象,直接用performSelector调用即可。
     */
#pragma  mark --  NSOperationQueue 
   // NSOperation 是一个抽象类,并不能直接使用,必须使用他的子类.本身无主线程,子线程之分,可在任意线程中使用,通常与 NSOperationQueue 结合使用
    
    //NSInvocationOperation
    NSInvocationOperation *invocationOp = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(invocationPrint) object:nil];
//    [invocationOp start];
//    [invocationOp release];
    
    //
    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
    
        NSLog(@"NSBlockOperation");
    }];
//    [blockOperation start];
//    [blockOperation release];
    
//    队列用于存放需要执行的操作(NSOperation)  放入队列中后其会自动执行,且NSInvocationOperation和NSBlockOperation不用执行父类的 start 的方法
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue addOperation:invocationOp];
    [queue addOperation:blockOperation];
    //队列中最大并发操作数量,默认是-1即不限数量,但实际 queue 会根据需要自动为 operation 开辟不超出最大数量的合适数量的线程
    queue.maxConcurrentOperationCount = 5;
    //结束的方法  -(viod)cancelAllOperations
    
}
-(void)invocationPrint
{
    NSLog(@"NSInvocationOperation");
}
- (IBAction)button2Action:(id)sender {

    [self performSelectorInBackground:@selector(downLoadImage:) withObject:@"http://www.lanou3g.com/uploadfile/2015/0102/20150102044232689.jpg"];
}

//子线程来调用下载图片
-(void)downLoadImage:(NSString *)urlString
{
    @autoreleasepool {
        //URL 下载相应的 data
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
        //将 data 转化为 image
        UIImage *image = [UIImage imageWithData:data];
        //跳至主线程刷新 UI 界面
        [self performSelectorOnMainThread:@selector(updataImageView:) withObject:image waitUntilDone:YES];
    
    }
}
//回到主线程,刷新 UIImageView 的图片
-(void)updataImageView:(UIImage *)image
{
    self.imageView.image = image;
}


-(void)print
{
    //主线程在 main 函数中,系统自动添加自动释放池
    //子线程,需要手动添加自动释放池,来释放 autorelease 对象
    @autoreleasepool {
        
    
    NSLog(@"current Thread:%@,isMainThread:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
    for (int i = 0; i <= 100000; i++) {
        NSLog(@"%d",i);
        if (10000 == i) {

            [NSThread exit];
        }
    }
  }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_imageView release];
    [super dealloc];
}
@end


你可能感兴趣的:(多线程,异步,子线程,NSThread总结,开辟线程)