1
2
3
4
5
|
//创建串行队列
var
serial:dispatch_queue_t = dispatch_queue_create(
"serialQueue1"
,
DISPATCH_QUEUE_SERIAL
)
//创建并行队列
var
concurrent:dispatch_queue_t = dispatch_queue_create(
"concurrentQueue1"
,
DISPATCH_QUEUE_CONCURRENT
)
|
1
|
var
globalQueue:dispatch_queue_t = dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0)
|
1
|
var
mainQueue:dispatch_queue_t = dispatch_get_main_queue()
|
1
2
3
4
5
6
7
8
9
10
|
//添加异步代码块到dispatch_get_global_queue队列
dispatch_async(dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0), { () ->
Void
in
//处理耗时操作的代码块...
println
(
"do work"
)
//操作完成,调用主线程来刷新界面
dispatch_async(dispatch_get_main_queue(), { () ->
Void
in
println
(
"main refresh"
)
})
})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//添加同步代码块到dispatch_get_global_queue队列
//不会造成死锁,当会一直等待代码块执行完毕
dispatch_sync(dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0), { () ->
Void
in
println
(
"sync1"
)
})
println
(
"end1"
)
//添加同步代码块到dispatch_get_main_queue队列
//会引起死锁
//因为在主线程里面添加一个任务,因为是同步,所以要等添加的任务执行完毕后才能继续走下去。但是新添加的任务排在
//队列的末尾,要执行完成必须等前面的任务执行完成,由此又回到了第一步,程序卡死
dispatch_sync(dispatch_get_main_queue(), { () ->
Void
in
println
(
"sync2"
)
})
println
(
"end2"
)
|
1
2
3
4
5
6
|
//创建并行队列
var
conQueue:dispatch_queue_t = dispatch_queue_create(
"concurrentQueue1"
,
DISPATCH_QUEUE_CONCURRENT
)
//暂停一个队列
dispatch_suspend(conQueue)
//继续队列
dispatch_resume(conQueue)
|
1
2
3
4
5
6
|
//往dispatch_get_global_queue队列中添加代码块,只执行一次
var
predicate:dispatch_once_t = 0
dispatch_once(&predicate, { () ->
Void
in
//只执行一次,可用于创建单例
println
(
"work"
)
})
|
1
2
3
4
5
6
|
//延时2秒执行
let
delta = 2.0 *
Double
(
NSEC_PER_SEC
)
let
dtime = dispatch_time(
DISPATCH_TIME_NOW
,
Int64
(delta))
dispatch_after(dtime, dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0)) { () ->
Void
in
println
(
"延时2秒执行"
)
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//获取系统存在的全局队列
var
queue:dispatch_queue_t = dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0)
//定义一个group
var
group:dispatch_group_t = dispatch_group_create()
//并发任务,顺序执行
dispatch_group_async(group, queue, {() ->
Void
in
println
(
"block1"
)
})
dispatch_group_async(group, queue, {() ->
Void
in
println
(
"block2"
)
})
dispatch_group_async(group, queue, {() ->
Void
in
println
(
"block3"
)
})
//所有任务执行结束汇总,不阻塞当前线程
dispatch_group_notify(group, dispatch_get_main_queue(), {() ->
Void
in
println
(
"group done"
)
})
//永久等待,直到所有任务执行结束,中途不能取消,阻塞当前线程
var
result = dispatch_group_wait(group,
DISPATCH_TIME_FOREVER
)
if
result == 0{
println
(
"任务全部执行完成"
)
}
else
{
println
(
"某个任务还在执行"
)
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//获取系统存在的全局队列
var
queue:dispatch_queue_t = dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0)
//定义一个一步代码块
dispatch_async(queue, {() ->
Void
in
//通过dispatch_apply,循环变量数组
dispatch_apply(6, queue, {(index) ->
Void
in
println
(index)
})
//执行完毕,主线程更新
dispatch_async(dispatch_get_main_queue(), {() ->
Void
in
println
(
"done"
)
})
})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
//获取系统存在的全局队列
var
queue:dispatch_queue_t = dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0)
//当并行执行的任务更新数据时,会产生数据不一样的情况
for
i
in
1...20
{
dispatch_async(queue,{ () ->
Void
in
println
(
"\(i)"
)
})
}
//使用信号量保证正确性
//创建一个初始计数值为1的信号
var
semaphore:dispatch_semaphore_t = dispatch_semaphore_create(1)
for
i
in
1...20
{
dispatch_async(queue,{ () ->
Void
in
//永久等待,直到Dispatch Semaphore的计数值 >= 1
dispatch_semaphore_wait(semaphore,
DISPATCH_TIME_FOREVER
)
println
(
"\(i)"
)
//发信号,使原来的信号计数值+1
dispatch_semaphore_signal(semaphore)
})
}
|