//
// ViewController.m
// test_addEntryDictionary_01
//
// Created by jeffasd on 16/6/26.
// Copyright © 2016年 jeffasd. All rights reserved.
//
#import "ViewController.h"
typedef int(^Myblock)(int, int);
typedef int(^TyBlock)(int, int);
@interface ViewController ()
@property (nonatomic, copy) TyBlock tyBlock;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [self testAsyncBLock1:^int(int a, int b) {
//
// NSLog(@" blcok is %@", [NSThread currentThread]);
//
// NSLog(@"asss");
//
// return a + b;
// }];
[self testSyncBlock:^int(int a, int b) {
NSLog(@" blcok is %@", [NSThread currentThread]);
NSLog(@"asss");
return a + b;
}];
NSLog(@"test who is first");
NSLog(@"--- thread is %@", [NSThread currentThread]);
NSLog(@"sencond");
[self testSync_01];
}
- (void)testSync_01{
NSLog(@"---------------------------------------------");
dispatch_queue_t q = dispatch_queue_create("cn.gcddemo", DISPATCH_QUEUE_CONCURRENT);
__block bool logon = NO;
// dispatch_sync(q, ^{
// NSLog(@"模拟耗时操作 %@", [NSThread currentThread]);
//
//// [NSThread sleepfortimeinterval:2.0f];
//
// [NSThread sleepForTimeInterval:2.0f];
//
// NSLog(@"模拟耗时完成 %@", [NSThread currentThread]);
// logon = YES;
// });
[NSThread sleepForTimeInterval:2.0f];
NSLog(@"模拟耗时完成 %@", [NSThread currentThread]);
dispatch_async(q, ^{
NSLog(@"登录完成的处理 %@", [NSThread currentThread]);
});
// [NSThread sleepForTimeInterval:2.0f];
//
// NSLog(@"登录完成的处理 %@", [NSThread currentThread]);
}
#pragma mark - 异步Block
- (void)testSyncBlock:(int(^)(int a, int b))myblock{
// myblock(2, 3);
// dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//
// myblock(2, 3);
//
// });
dispatch_queue_t serialQueue = dispatch_queue_create("serial", NULL);
dispatch_queue_t concurrent = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);
// dispatch_sync(serialQueue, ^{
//
// myblock(2, 3);
//
// });
// dispatch_sync(concurrent, ^{
//
// myblock(2, 3);
//
// });
#if 1
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// myblock(2, 3);
// dispatch_sync(dispatch_get_main_queue(), ^{
//
// myblock(2, 3);
// });
dispatch_queue_t serialQueue = dispatch_queue_create("serial", NULL);
dispatch_queue_t concurrent = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(serialQueue, ^{
myblock(2, 3);
});
// dispatch_sync(concurrent, ^{
//
// myblock(2, 3);
//
// });
});
#endif
}
- (void)testAsyncBLock1:(int(^)(int a, int b))myblock{
// dispatch_async(dispatch_get_main_queue(), ^{
//
// myblock(2, 3);
// });
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//
// myblock(2, 3);
// });
dispatch_queue_t serialQueue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);
// dispatch_async(serialQueue, ^{
//
// myblock(2, 3);
//
// });
dispatch_async(concurrentQueue, ^{
myblock(2, 3);
});
}
- (void)testAsyncFounction{
NSLog(@"--- thread is %@", [NSThread currentThread]);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"thread is %@", [NSThread currentThread]);
NSLog(@"sdfsf");
});
dispatch_queue_t serialQueue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(serialQueue, ^{
NSLog(@"thread is %@", [NSThread currentThread]);
NSLog(@"sdfsf");
});
dispatch_async(concurrentQueue, ^{
NSLog(@"thread is %@", [NSThread currentThread]);
NSLog(@"sdfsf");
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"thread is %@", [NSThread currentThread]);
NSLog(@"sdfsf");
});
NSLog(@"sencond");
}
- (void)testSyncFounction{
NSLog(@"--- thread is %@", [NSThread currentThread]);
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"thread is %@", [NSThread currentThread]);
NSLog(@"sdfsf");
});
dispatch_queue_t serialQueue = dispatch_queue_create("serial", NULL);
dispatch_queue_t concurrent = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(serialQueue, ^{
NSLog(@"thread is %@", [NSThread currentThread]);
NSLog(@"sdfsf");
});
dispatch_sync(concurrent, ^{
NSLog(@"thread is %@", [NSThread currentThread]);
NSLog(@"sdfsf");
});
NSLog(@"sencond");
}
- (void)test{
NSMutableDictionary *mutDic = [@{
@"name" : @"jack",
@"price" : @"12.8",
@"height" : @34,
@"width" : @11
} mutableCopy];
NSDictionary *newDic = @{
@"name" : @"111",
@"price" : @"1111",
@"address" : @"zhongshan"
};
[mutDic addEntriesFromDictionary:newDic];
NSLog(@"the dic is %@", mutDic);
int result = 0;
result = [self testBlockResult:^int(int a, int b) {
NSLog(@" is first call back ---");
return a + b;
}];
NSLog(@"the result is %d", result);
// self testBlockTypedef:^int(int, int) {
//
// }
self.tyBlock = ^int(int a, int b){
NSLog(@"a + b");
return a + b;
};
}
#pragma mark - 同步Block 相当于函数调用
- (int)testBlockResult:(int(^)(int a, int b))myBlcok{
return myBlcok(3, 5);
// dispatch_async(dispatch_get_main_queue(), ^{
//
// myBlcok(3, 5);
// });
//
// return 3;
}
- (int)testBlock1:(void(^)(int, int))myBlock{
return 2;
}
- (int)testBlcokInfo:(void(^)(NSString *, NSString *))myBlock{
return 3;
}
- (int)testBlockTypedef:(Myblock)myblock{
return myblock(2, 4);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
self.tyBlock(5, 7);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
iOS block 详解
最新推荐文章于 2024-10-28 23:04:59 发布