多线程

  1. - (void)dead  
  2. {  
  3.     // NSThread提供的一些方法  
  4.       
  5.     // 1.获取当前的线程  
  6.       
  7.     NSThread *currentThread = [NSThread currentThread];  
  8.     NSLog(@"当前线程:%@", currentThread);  
  9.       
  10.     // 2.获取主线程  
  11.       
  12.     NSThread *mainThread = [NSThread mainThread];  
  13.     NSLog(@"主线程:%@", mainThread);  
  14.       
  15.     // 3.判断当前的线程是不是主线程  
  16.       
  17.     BOOL result = [NSThread isMainThread];  
  18.     NSLog(@"是不是主线程:%d", result);  
  19.       
  20.     // 4.让当前的线程休眠几秒  
  21. //    [NSThread sleepForTimeInterval:2];  
  22.       
  23.     int a = 0;  
  24.       
  25.     NSLog(@"开始");  
  26.     for (int i = 0 ; i < 600000000;  i++) {  
  27.         a++;  
  28.     }  
  29.       
  30.     NSLog(@"%d", a);  
  31.       
  32. }

NSObject

  1. - (IBAction)NSObjectAction:(id)sender {  
  2.       
  3. //    [self dead];  
  4.       
  5.     // 1.第一种多线程的实现方式  
  6.       
  7.     // NSObject自带的多线程实现  
  8.       
  9.       
  10.     // 优点:写法简单  
  11.       
  12.     // 缺点:对于线程的安全没有做处理  
  13.       
  14.     [self performSelectorInBackground:@selector(dead) withObject:nil];  
  15.       
  16.       
  17.       
  18. }

NSThread

[html]  view plain copy
  1. - (IBAction)NSTheadAction:(id)sender {  
  2.       
  3.     // 2.NSThread类 线程类,这个类的一个对象就代表一个线程  
  4.       
  5.     // 创建一个对象就相当于创建一个线程  
  6.       
  7.       
  8.     // 优点:可以对线程本身进行设置操作  
  9.     // 缺点:使用太繁琐,线程的安全也需要开发者自己处理  
  10.   
  11.     NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(dead) object:nil];  
  12.       
  13.     thread.name = @"随便";  
  14.       
  15.       
  16.     // 开始线程  
  17.     [thread start];  
  18.       
  19.     [thread release];  
  20.       
  21.       
  22. }  

NSOperation

[html] view plaincopy
  1. - (IBAction)NSOperationAction:(id)sender {  
  2.       
  3.     // 3.NSOperation类 代表一个任务  
  4.       
  5.     // 这个类的对象本身没有任何多线程操作,执行的时候会自动放进一个当前的线程  
  6.     // 这个类不能直接使用,需要创建子类使用  
  7.       
  8.     MyOperation *op = [[MyOperation alloc] init];  
  9.       
  10.     // 开始执行,会在当前的线程直接执行  
  11.     [op start];  
  12.       
  13.     [op release];  
  14.       
  15.     // 系统提供了两个子类  
  16.       
  17. //    NSInvocationOperation  
  18. //    NSBlockOperation  
  19.       
  20.       
  21.       
  22. }  

NSOperationQueue

[html] view plaincopy
  1. - (IBAction)NSOperationQueueAction:(id)sender {  
  2.       
  3.     // NSOperationQueue 操作队列, 作用:管理一系列的线程和任务  
  4.       
  5.     // 机制:不断的将任务分配给线程,当线程执行完毕,会回到线程池中等待任务分配  
  6.       
  7.     // 优点:节省系统的资源利用,能够很好的规划线程的使用,不需要关心线程的安全问题  
  8.     // 缺点:没有  
  9.       
  10.     NSOperationQueue *queue = [[NSOperationQueue alloc] init];  
  11.       
  12.     // 主队列,一个串行队列,只管理一个线程:主线程  
  13.     [NSOperationQueue mainQueue];  
  14.       
  15.     // 最大并发数  
  16.     [queue setMaxConcurrentOperationCount:2];  
  17.       
  18.     // 给队列里添加任务  
  19.       
  20. //    MyOperation *op1 = [[MyOperation alloc] init];  
  21. //    [queue addOperation:op1];  
  22. //    [op1 release];  
  23.       
  24.     [queue addOperationWithBlock:^{  
  25.         [self dead];  
  26.     }];  
  27.     [queue addOperationWithBlock:^{  
  28.         [self dead];  
  29.     }];  
  30.     [queue addOperationWithBlock:^{  
  31.         [self dead];  
  32.     }];  
  33.     [queue addOperationWithBlock:^{  
  34.         [self dead];  
  35.     }];  
  36.       
  37. }  

GCD

[html] view plaincopy
  1. - (IBAction)GCDAction:(id)sender {  
  2.       
  3.     // GCD  
  4.       
  5.     // 创建一个队列  
  6.     // 参数1:队列名  
  7.     // 参数2:队列的类型  
  8.     //DISPATCH_QUEUE_CONCURRENT  并行  
  9.     //DISPATCH_QUEUE_SERIAl      串行  
  10.     dispatch_queue_t myQueue = dispatch_queue_create("lizhi", DISPATCH_QUEUE_CONCURRENT);  
  11.       
  12.     // 在队列中分配任务  
  13.       
  14.     // 参数1:在哪个队列中分配任务  
  15.     // 参数2:在block中写要执行的内容  
  16. //    dispatch_async(myQueue, ^{  
  17. //        [self dead];  
  18. //    });  
  19.       
  20.       
  21.     // 系统提供了5个队列供开发者使用:全都是单例  
  22.       
  23.     // 1个串行队列,叫主队列,负责对主线程进行调度处理  
  24.     // 4个并行队列,叫全局对列,负责调度所有的子线程  
  25.       
  26.       
  27.     // 1.获得主队列  
  28.     dispatch_queue_t mainQueue = dispatch_get_main_queue();  
  29.       
  30. //    dispatch_async(mainQueue, ^{  
  31. //        [self dead];  
  32. //    });  
  33.       
  34.       
  35.     // 2.获得全局队列  
  36.     // 参数1:获取哪一个全局队列, 有4个不同的优先级  
  37.     dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);  
  38.       
  39.       
  40.     dispatch_async(globalQueue, ^{  
  41.         [self dead];  
  42.     });  
  43.     dispatch_async(globalQueue, ^{  
  44.         [self dead];  
  45.     });  
  46.     dispatch_async(globalQueue, ^{  
  47.         [self dead];  
  48.     });  
  49.     dispatch_async(globalQueue, ^{  
  50.         [self dead];  
  51.     });  
  52.       
  53.       
  54.     // 原则上,所有对数据的处理,都需要开辟子线程进行  
  55.       
  56.       
  57.       
  58.       
  59.     // GCD的常用写法  
  60.     // 在子线程申请数据  
  61.     dispatch_async(globalQueue, ^{  
  62.           
  63.         NSString *str = @"http://pic4.nipic.com/20090922/1963767_165747613683_2.jpg";  
  64.           
  65.         // 利用NSData的一个同步方法  
  66.         NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];  
  67.           
  68.         // 将数据转为一个图片  
  69.         UIImage *image = [UIImage imageWithData:data];  
  70.           
  71.         dispatch_async(mainQueue, ^{  
  72.              
  73.             // 在主线程中写对UI界面的处理,赋值,刷新。。。  
  74.               
  75.             self.imageView.image = image;  
  76.               
  77.               
  78.         });  
  79.           
  80.           
  81.     });  
  82.       
  83.       
  84.     // 延时执行  
  85.    
  86.     NSLog(@"开始延时");  
  87.     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{  
  88.         NSLog(@"延时执行");  
  89.     });  
  90.       
  91.       
  92.     // 单例执行  
  93.     static dispatch_once_t onceToken;  
  94.     dispatch_once(&onceToken, ^{  
  95.         NSLog(@"只执行一次");  
  96.     });  
  97.       
  98. }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值