示例源码清单如下:
1、SingletonSample.h
//
// SingletonSample.h
// BlockSample
//
// Created by developer on 13-9-27.
// Copyright (c) 2013年 developer. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface SingletonSample : NSObject
+ (id)ShareInstance;
@end
2、SingletonSample.m
//
// SingletonSample.m
// BlockSample
//
// Created by developer on 13-9-27.
// Copyright (c) 2013年 developer. All rights reserved.
//
#import "SingletonSample.h"
@implementation SingletonSample
+ (id)ShareInstance{
static SingletonSample *SharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SharedInstance = [[SingletonSample alloc] init];
});
return SharedInstance;
}
@end
3、BlockSampleTests.h
//
// BlockSampleTests.h
// BlockSampleTests
//
// Created by developer on 13-8-3.
// Copyright (c) 2013年 developer. All rights reserved.
//
#import <SenTestingKit/SenTestingKit.h>
@interface BlockSampleTests : SenTestCase
@end
4、BlockSampleTests.m
//
// BlockSampleTests.m
// BlockSampleTests
//
// Created by developer on 13-8-3.
// Copyright (c) 2013年 developer. All rights reserved.
//
#import "SingletonSample.h"
#import "BlockSampleTests.h"
static int global = 100;
static volatile BOOL flag = NO;
static const int Length = 100;
static int data[Length];
static void initData()
{
for(int i = 0; i < Length; i++)
data[i] = i + 1;
}
@implementation BlockSampleTests
- (void)setUp
{
[super setUp];
// Set-up code here.
}
- (void)tearDown
{
// Tear-down code here.
[super tearDown];
}
- (void)test1
{
// 默认打印一条语句"Hello, World!"
NSLog(@"Hello, World!");
}
- (void)testBlock1
{
// block 打印一条语句"Hello, World!"
void (^aBlock)(void) = ^(void){ NSLog(@"Hello, World!"); };
aBlock();
}
- (void)testBlock2
{
// block 打印一条语句"Hello, World!"
void (^aBlock)(void) = 0;
aBlock = ^(void) {
NSLog(@"Hello, World!");
};
aBlock();
}
- (void)testBlockArray
{
// block 数组
void (^blocks[2])(void) = {
^(void){ NSLog(@" >> This is block 1!"); },
^(void){ NSLog(@" >> This is block 2!"); }
};
blocks[0]();
blocks[1]();
}
- (void)testBlock4
{
/*
block 是分配在 stack 上的,这意味着我们必须小心里处理 block 的生命周期。
比如如下的做法是不对的,因为 stack 分配的 block 在 if 或 else 内是有效的,但是到大括号 } 退出时就可能无效了:
*/
dispatch_block_t block;
BOOL x = 1;
if (x) {
block = ^{ printf("true\n"); };
} else {
block = ^{ printf("false\n"); };
}
block();
}
- (void)testBlock5
{
/*
考虑到 block 的目的是为了支持并行编程,对于普通的 local 变量,我们就不能在 block 里面随意修改(原因很简单,block 可以被多个线程并行运行,会有问题的)
而且如果你在 block 中修改普通的 local 变量,编译器也会报错。
那么该如何修改外部变量呢?有两种办法:
第一种是可以修改 static 全局变量;
第二种是可以修改用新关键字 __block 修饰的变量。
*/
__block int blockLocal = 100;
static int staticLocal = 100;
void (^aBlock)(void) = ^(void){
NSLog(@" >> Sum: %d\n", global + staticLocal);
global++;
blockLocal++;
staticLocal++;
};
aBlock();
NSLog(@"After modified, global: %d, block local: %d, static local: %d\n", global, blockLocal, staticLocal);
}
- (void)testBlock6
{
/*
我们也可以引用 static block 或 __block block。比如我们可以用他们来实现 block 递归
*/
void (^aBlock)(int) = 0;
static void (^ const staticBlock)(int) = ^(int i) {
if (i > 0) {
NSLog(@" >> static %d", i);
staticBlock(i - 1);
}
};
aBlock = staticBlock;
aBlock(5);
// 2
__block void (^blockBlock)(int);
blockBlock = ^(int i) {
if (i > 0) {
NSLog(@" >> block %d", i);
blockBlock(i - 1);
}
};
blockBlock(5);
}
- (void)testBlock7
{
/*
上面我们介绍了 block 及其基本用法,但还没有涉及并行编程。
block 与 Dispatch Queue 分发队列结合起来使用,是 iOS 中并行编程的利器。
*/
// create dispatch queue
dispatch_queue_t queue = dispatch_queue_create("StudyBlocks", NULL);
dispatch_async(queue, ^(void) {
int sum = 0;
for(int i = 0; i < Length; i++)
sum += data[i];
NSLog(@" >> Sum: %d", sum);
flag = YES;
});
// wait util work is done.
while (!flag);
dispatch_release(queue);
}
- (void)testBlock8
{
/*
在上面的例子中,我们的主线程一直在轮询 flag 以便知晓 block 线程是否执行完毕,这样做的效率是很低的,严重浪费 CPU 资源。
我们可以使用一些通信机制来解决这个问题,如:semaphore(信号量)。
semaphore 的原理很简单,就是生产-消费模式,必须生产一些资源才能消费,没有资源的时候,那我就啥也不干,直到资源就绪。
*/
initData();
// Create a semaphore with 0 resource
__block dispatch_semaphore_t sem = dispatch_semaphore_create(0);
// create dispatch semaphore
dispatch_queue_t queue = dispatch_queue_create("StudyBlocks", NULL);
dispatch_async(queue, ^(void) {
int sum = 0;
for(int i = 0; i < Length; i++)
sum += data[i];
NSLog(@" >> Sum: %d", sum);
// signal the semaphore: add 1 resource
dispatch_semaphore_signal(sem);
});
// wait for the semaphore: wait until resource is ready.
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_release(sem);
dispatch_release(queue);
}
- (void)testBlock9
{
/*
下面我们来看一个按照 FIFO 顺序执行并用 semaphore 同步的例子:先将数组求和再依次减去数组。
*/
initData();
__block int sum = 0;
// Create a semaphore with 0 resource
__block dispatch_semaphore_t sem = dispatch_semaphore_create(0);
__block dispatch_semaphore_t taskSem = dispatch_semaphore_create(0);
// create dispatch semaphore
dispatch_queue_t queue = dispatch_queue_create("StudyBlocks", NULL);
dispatch_block_t task1 = ^(void) {
int s = 0;
for (int i = 0; i < Length; i++)
s += data[i];
sum = s;
NSLog(@" >> after add: %d", sum);
dispatch_semaphore_signal(taskSem);
};
dispatch_block_t task2 = ^(void) {
dispatch_semaphore_wait(taskSem, DISPATCH_TIME_FOREVER);
int s = sum;
for (int i = 0; i < Length; i++)
s -= data[i];
sum = s;
NSLog(@" >> after subtract: %d", sum);
dispatch_semaphore_signal(sem);
};
dispatch_async(queue, task1);
dispatch_async(queue, task2);
// wait for the semaphore: wait until resource is ready.
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_release(taskSem);
dispatch_release(sem);
dispatch_release(queue);
/*
在上面的代码中,我们利用了 dispatch_queue 的 FIFO 特性,
确保 task1 先于 task2 执行,而 task2 必须等待直到 task1 执行完毕才开始干正事,主线程又必须等待 task2 才能干正事。
这样我们就可以保证先求和,再相减,然后再让主线程运行结束这个顺序。
*/
}
- (void)testBlock10
{
/*
使用 dispatch_apply 进行并发迭代:
对于上面的求和操作,我们也可以使用 dispatch_apply 来简化代码的编写:
*/
initData();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
__block int sum = 0;
__block int *pArray = data;
// iterations
dispatch_apply(Length, queue, ^(size_t i) {
sum += pArray[i];
});
NSLog(@" >> sum: %d", sum);
dispatch_release(queue);
/*
注意这里使用了全局 dispatch_queue。
dispatch_apply 的定义如下:
dispatch_apply(size_t iterations, dispatch_queue_t queue, void (^block)(size_t));
参数 iterations 表示迭代的次数,void (^block)(size_t) 是 block 循环体。这么做与 for 循环相比有什么好处呢?
答案是:并行,这里的求和是并行的,并不是按照顺序依次执行求和的。
*/
}
- (void)testBlock11
{
/*
我们可以将完成一组相关任务的 block 添加到一个 dispatch group 中去,这样可以在 group 中所有 block 任务都完成之后,再做其他事情。
比如 testBlock9 中的示例也可以使用 dispatch group 实现
*/
initData();
__block int sum = 0;
// Create a semaphore with 0 resource
__block dispatch_semaphore_t taskSem = dispatch_semaphore_create(0);
// create dispatch semaphore
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("StudyBlocks", NULL);
dispatch_block_t task1 = ^(void) {
int s = 0;
for (int i = 0; i < Length; i++)
s += data[i];
sum = s;
NSLog(@" >> after add: %d", sum);
dispatch_semaphore_signal(taskSem);
};
dispatch_block_t task2 = ^(void) {
dispatch_semaphore_wait(taskSem, DISPATCH_TIME_FOREVER);
int s = sum;
for (int i = 0; i < Length; i++)
s -= data[i];
sum = s;
NSLog(@" >> after subtract: %d", sum);
};
// Fork
dispatch_group_async(group, queue, task1);
dispatch_group_async(group, queue, task2);
// Join
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
dispatch_release(taskSem);
dispatch_release(queue);
dispatch_release(group);
}
- (void)testBlock12
{
/*
“带有自动变量值”在block中表现为“截获自动变量”
即保存自动变量的值
所以在执行block语法后,即使改写block中使用的自动变量的值也不会影响block执行时自动变量的值
该源码就在block语法后改写了block中自动变量val和fmt
但执行结果是:
val = 10
*/
int val = 10;
const char *fmt = "val = %d\n";
void (^blk)(void) = ^{printf(fmt, val);};
val = 2;
fmt = "These values were changed. val = %d\n";
blk(); // 执行结果是:val = 10
}
- (void)testBlock13{
// 虽然我们把Block Objects 异步分派到了串行队列上,这个还是按照FIFO原则执行它的代码
__block dispatch_semaphore_t sem1 = dispatch_semaphore_create(0);
__block dispatch_semaphore_t sem2 = dispatch_semaphore_create(0);
__block dispatch_semaphore_t sem3 = dispatch_semaphore_create(0);
dispatch_queue_t firstSerialQueue = dispatch_queue_create("com.launch.GCD.serialQueue1", 0);
dispatch_async(firstSerialQueue, ^{
NSUInteger counter = 0;
for (counter=0; counter<5; counter++) {
NSLog(@"First interation, counter=%d", counter);
}
dispatch_semaphore_signal(sem1);
});
dispatch_async(firstSerialQueue, ^{
NSUInteger counter = 0;
for (counter=0; counter<5; counter++) {
NSLog(@"Second interation, counter=%d", counter);
}
dispatch_semaphore_signal(sem2);
});
dispatch_async(firstSerialQueue, ^{
NSUInteger counter = 0;
for (counter=0; counter<5; counter++) {
NSLog(@"Third interation, counter=%d", counter);
}
dispatch_semaphore_signal(sem3);
});
dispatch_semaphore_wait(sem1, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(sem2, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(sem3, DISPATCH_TIME_FOREVER);
dispatch_release(firstSerialQueue);
dispatch_release(sem1);
dispatch_release(sem2);
dispatch_release(sem3);
}
- (void)testBlock14{
// 虽然我们把Block Objects 异步分派到了串行队列上,这个还是按照FIFO原则执行它的代码
// 但是我们可以创建多个串行队列,不同串行队列之间是并行的
__block dispatch_semaphore_t sem1 = dispatch_semaphore_create(0);
__block dispatch_semaphore_t sem2 = dispatch_semaphore_create(0);
__block dispatch_semaphore_t sem3 = dispatch_semaphore_create(0);
dispatch_queue_t firstSerialQueue1 = dispatch_queue_create("com.launch.GCD.serialQueue1", 0);
dispatch_queue_t firstSerialQueue2 = dispatch_queue_create("com.launch.GCD.serialQueue2", 0);
dispatch_queue_t firstSerialQueue3 = dispatch_queue_create("com.launch.GCD.serialQueue3", 0);
dispatch_async(firstSerialQueue1, ^{
NSUInteger counter = 0;
for (counter=0; counter<5; counter++) {
NSLog(@"First interation, counter=%d", counter);
}
dispatch_semaphore_signal(sem1);
});
dispatch_async(firstSerialQueue2, ^{
NSUInteger counter = 0;
for (counter=0; counter<5; counter++) {
NSLog(@"Second interation, counter=%d", counter);
}
dispatch_semaphore_signal(sem2);
});
dispatch_async(firstSerialQueue3, ^{
NSUInteger counter = 0;
for (counter=0; counter<5; counter++) {
NSLog(@"Third interation, counter=%d", counter);
}
dispatch_semaphore_signal(sem3);
});
dispatch_semaphore_wait(sem1, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(sem2, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(sem3, DISPATCH_TIME_FOREVER);
dispatch_release(firstSerialQueue1);
dispatch_release(firstSerialQueue2);
dispatch_release(firstSerialQueue3);
dispatch_release(sem1);
dispatch_release(sem2);
dispatch_release(sem3);
}
static dispatch_once_t onceToken;
void (^executedOnlyOnce)(void) = ^{
static NSUInteger numberOfEntries = 0;
numberOfEntries++;
NSLog(@"Executed %d time(s)", numberOfEntries);
};
- (void)testBlock15{
// 一个任务最多只执行一次
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_once(&onceToken, ^{
dispatch_async(concurrentQueue, executedOnlyOnce);
});
dispatch_once(&onceToken, ^{
dispatch_async(concurrentQueue, executedOnlyOnce);
});
// 稍缓线程退出
NSUInteger count= 0;
while (count++ < 3) {
sleep(1);
}
}
- (void)testBlock16{
// 利用dispatch_once实现单例
/*
@interface SingletonSample : NSObject
+ (id)ShareInstance;
@end
@implementation SingletonSample
+ (id)ShareInstance{
static SingletonSample *SharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SharedInstance = [[SingletonSample alloc] init];
});
return SharedInstance;
}
@end
*/
SingletonSample *obj1 = [SingletonSample ShareInstance];
SingletonSample *obj2 = [SingletonSample ShareInstance];
NSLog(@"obj1=%@, obj2=%@", obj1, obj2);
STAssertEquals(obj1, obj2, nil);
}
- (void)testBlock17{
// 利用GCD延时后执行任务
double delayInsenconds = 2.0;
dispatch_time_t delayInNanoSeconds = dispatch_time(DISPATCH_TIME_NOW, delayInsenconds * NSEC_PER_SEC);
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSLog(@"testBlock17");
dispatch_after(delayInNanoSeconds, concurrentQueue, ^(void){
NSLog(@"Now do work in dispatch_after");
});
// 稍缓线程退出
NSUInteger count= 0;
while (count++ < 5) {
sleep(1);
}
}
@end