iOS 多线程

来源:互联网 发布:一元云购源码 犯法吗 编辑:程序博客网 时间:2024/06/02 04:54
下面是多线程的一些基本方法


- (IBAction)GcdButton:(UIButton*)sender {
   
   
//GCD多线程分两种方式:1.串行队列2.并行队列
   
//串行队列特点:任务是一个一个的执行,当一个任务执行完毕再执行另一个,多个任务不是同时进行,而是一个一个的执行.
   
//并行队列:多个任务可以同时执行,谁先完成任务不一定,提高任务的执行效率,是工作中很常用的一种方式
   
   
//串行队列的使用:
   
//1.创建主线程队列
   
/*
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
   
    //
将任务放在队列中执行
    dispatch_async(mainQueue, ^{
       
        NSLog(@"
是否是主线程1 = %d",[NSThread isMainThread]);
       
    });
    dispatch_async(mainQueue, ^{
       
        NSLog(@"
是否是主线程2 = %d",[NSThread isMainThread]);
       
    });
    dispatch_async(mainQueue, ^{
       
        NSLog(@"
是否是主线程3 = %d",[NSThread isMainThread]);
       
    });
    dispatch_async(mainQueue, ^{
       
        NSLog(@"
是否是主线程4 = %d",[NSThread isMainThread]);
       
    });
    */

   
   
//自定义串行队列
   
//参数1.队列标识符,通常以公司逆向域名形式命名
   
//参数2.队列类型DISPATCH_QUEUE_SERIAL是串行队列类型
   
/*
    dispatch_queue_t serialQueue = dispatch_queue_create("com.lanou3g.GCD.Serial", DISPATCH_QUEUE_SERIAL);
   
    dispatch_async(serialQueue, ^{
       
        NSLog(@"
是否是主线程1 = %d",[NSThread isMainThread]);
       
    });
    dispatch_async(serialQueue, ^{
       
        NSLog(@"
是否是主线程2= %d",[NSThread isMainThread]);
       
    });
    dispatch_async(serialQueue, ^{
       
        NSLog(@"
是否是主线程3 = %d",[NSThread isMainThread]);
       
    });
    dispatch_async(serialQueue, ^{
       
        NSLog(@"
是否是主线程4 = %d",[NSThread isMainThread]);
       
    });
    */

   
   
//系统的并行队列
   
//参数1.并行队列执行优先级
   
//参数2.备用参数设置为0
   
/*
    dispatch_queue_t golBalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(golBalQueue, ^{
       
        NSLog(@"
是否是主线程1 = %d",[NSThread isMainThread]);
       
    });
    dispatch_async(golBalQueue, ^{
       
        NSLog(@"
是否是主线程2= %d",[NSThread isMainThread]);
       
    });
    dispatch_async(golBalQueue, ^{
       
        NSLog(@"
是否是主线程3 = %d",[NSThread isMainThread]);
       
    });
    dispatch_async(golBalQueue, ^{
       
        NSLog(@"
是否是主线程4 = %d",[NSThread isMainThread]);
       
    });
    */

   
   
//自定义并行队列
   
//参数1.队列标识符
   
//参数2.队列类型
   
/*
    dispatch_queue_t conCurrentQueue = dispatch_queue_create("com.lanou3g.GCD.ConCurrent", DISPATCH_QUEUE_CONCURRENT);
    //
执行队列
    dispatch_async(conCurrentQueue, ^{
       
        dispatch_async(conCurrentQueue, ^{
           
            NSLog(@"
是否是主线程1 = %d",[NSThread isMainThread]);
           
        });
        dispatch_async(conCurrentQueue, ^{
           
            NSLog(@"
是否是主线程2= %d",[NSThread isMainThread]);
           
        });
        dispatch_async(conCurrentQueue, ^{
           
            NSLog(@"
是否是主线程3 = %d",[NSThread isMainThread]);
           
        });
        dispatch_async(conCurrentQueue, ^{
           
            NSLog(@"
是否是主线程4 = %d",[NSThread isMainThread]);
           
        });
    });
    */

   
   
//http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg
   
   
//创建子线程异步并行,回到主线程,刷新数据
   
/*
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      
        NSURL *url = [NSURL URLWithString:@"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        dispatch_async(dispatch_get_main_queue(), ^{
           //
回到主线程,刷新数据
            self.MyimageView.image = [UIImage imageWithData:data];
           
        });
    });
    */

   
   
//延迟多少秒执行队列里的任务(上拉下拉偏移量的延迟)
   
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       
       
       
NSLog(@"延迟三秒我才被打印");
    });
   
   
//只执行一次(单例)
   
static dispatch_once_t onceToken;
   
dispatch_once(&onceToken, ^{
       
       
NSLog(@"我只被打印一次");
       
    });
   
}
@end

单例最新的写法
+(instancetype)ShareSingleModel{
   
   
//不同的子线程可能同时走 会创建多个单例对象
   
//也可以采用枷锁或解锁()
   
static SingleModel *singleModel =nil;
   
   
//不管有多少个线程都只走一次(以后的写法)
   
static dispatch_once_t onceToken;
   
dispatch_once(&onceToken, ^{
       
        singleModel = [[
SingleModelalloc]init];
       
    });
   
   
return singleModel;
   
/*
    
以前的写法
    if (singleModel == nil) {
        singleModel = [[SingleModel alloc] init];
    }
    return singleModel;
     */

}

//解决其他多线程方法(除了GCD)的多线程问题
//对线程进行加锁/解锁,会占用很大的系统资源
+(
instancetype)shareSingleM{
   
static SingleModel *singleM =nil;
   
   
@synchronized(self){
       
if (singleM == nil) {
            singleM = [[[
selfclass]alloc]init];
        }
    }
   
   
return singleM;
}
0 0
原创粉丝点击