多线程:一次性执行

//
//  ViewController.m
//  13-一次性执行
//
//  Created by gzxzmac on 16/1/29.
//  Copyright © 2016年 gzxzmac. All rights reserved.
//

#import "ViewController.h"
#import "CZTool.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 设置导航栏样式
    });
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
   /*
    CZTool *tool0 = [CZTool shareTool];
    CZTool *tool1 = [CZTool shareTool];
    CZTool *tool2 = [CZTool shareTool];
    CZTool *tool3 = [CZTool shareTool];
    NSLog(@"%@ , %@ , %@ , %@",tool0, tool1, tool2, tool3);

    CZTool *tool4 = [CZTool syncTool];
    CZTool *tool5 = [CZTool syncTool];
    CZTool *tool6 = [CZTool syncTool];
    NSLog(@"%@, %@ , %@",tool4, tool5, tool6);
    */

    CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
    for (int i = 0; i < 1000 * 1000; ++i) {
      CZTool *tool = [CZTool shareTool];
    }
    NSLog(@"一次性执行的单例 : %f",CFAbsoluteTimeGetCurrent() - start);
    start = CFAbsoluteTimeGetCurrent();
    for (int i = 0; i < 1000 * 1000; ++i) {
        CZTool *tool = [CZTool syncTool];
    }
    NSLog(@"互斥锁的单例 : %f",CFAbsoluteTimeGetCurrent() - start);


}

- (void)onceDemo {
    NSLog(@"start");
    static dispatch_once_t onceToken;
    // 当 onceToken 是 0 的时候才会执行block 里面的代码
    //    NSLog(@"默认值%ld",onceToken);
    static NSObject *obj;
    //    static NSString *ID = @"Cell";
    dispatch_once(&onceToken, ^{
        // 写一次性执行的代码
        NSLog(@"真的是一次吗?");
        obj = [[NSObject alloc]init];
    });
    NSLog(@"%@",obj);
    //    NSLog(@"%ld",onceToken);
    NSLog(@"end");

    /*
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
        // 放一次性执行的代码
     });
     */
}

@end
#import <Foundation/Foundation.h>

/*
 1. 此类在程序中只有一个对象
 2. 有提供类方法来获取单例对象
 */
@interface CZTool : NSObject
+ (instancetype)shareTool; // 使用一次性执行来实现单例
+ (instancetype)syncTool;// 使用 互斥锁 来实现单例
@end



#import "CZTool.h"

@implementation CZTool

// 工作中单例通常使用一次性执行来完成
+ (instancetype)shareTool {
    static dispatch_once_t onceToken;
    // 通常(写明确的类型也可以)
    static id instance;
    // 内部也有锁,性能比互斥锁要高
    dispatch_once(&onceToken, ^{
        instance = [[self alloc]init];
    });
    return instance;
}

// 之前有看到提供重置单例


+ (instancetype)syncTool {
    static id instance;
    // 在同一时间内,只会执行一次
    @synchronized(self) {
        // 同一时间,只有一个调用会来到这里
        if (instance == nil) {
            instance = [[self alloc]init];
        }
    }
    return instance;
}
@end
#import <Foundation/Foundation.h>

@interface CZTest : NSObject

@end



#import "CZTest.h"

@implementation CZTest
static dispatch_once_t onceToken;
// 通常(写明确的类型也可以)
static id instance;

+ (instancetype)shareTool {
    // 内部也有锁,性能比互斥锁要高
    dispatch_once(&onceToken, ^{
        instance = [[self alloc]init];
    });
    return instance;
}

// 之前有看到提供重置单例(CoreData)
+ (void)reset {
    onceToken = 0;
    instance = nil;
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值