您的位置:首页 > 移动开发 > Swift

swift和oc gcd使用

2018-03-07 11:23 288 查看
最近在做oc转swift项目,碰到swift中gcd的用法,整理了下gcd常用方法,希望对大家有帮助
///并行队列 + 异步执行,可同时开启多线程,任务交替执行
func asyncConcurrent() {
/* swift */
print("asyncConcurrent---begin");
let queue = DispatchQueue(label: "com.lin.syc")
queue.async {
for i in 0..<2 {
print("-------",i)
}
}
queue.async {
for i in 3..<5 {
print("-------",i)
}
}
/* oc */
//        dispatch_queue_t queue= dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT);
//
//        dispatch_async(queue, ^{
//            for (int i = 0; i < 2; ++i) {
//                NSLog(@"1------%@",[NSThread currentThread]);
//            }
//            });
//        dispatch_async(queue, ^{
//            for (int i = 0; i < 2; ++i) {
//                NSLog(@"2------%@",[NSThread currentThread]);
//            }
//            });
//        dispatch_async(queue, ^{
//            for (int i = 0; i < 2; ++i) {
//                NSLog(@"3------%@",[NSThread currentThread]);
//            }
//            });
print("asyncConcurrent---end");
}

///串行队列 + 同步执行,不会开启新线程,在当前线程执行任务。任务是串行的,执行完一个任务,再执行下一个任务
func syncSerial() {
print("syncSerial---begin");
/* swift */
let queue = DispatchQueue(label: "com.lin.syc")
queue.sync {
for i in 0..<2 {
print("-------",i)
}
}
queue.sync {
for i in 3..<5 {
print("-------",i)
}
}
/* oc */
//        dispatch_queue_t queue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_SERIAL);
//
//        dispatch_sync(queue, ^{
//            for (int i = 0; i < 2; ++i) {
//                NSLog(@"1------%@",[NSThread currentThread]);
//            }
//            });
//        dispatch_sync(queue, ^{
//            for (int i = 0; i < 2; ++i) {
//                NSLog(@"2------%@",[NSThread currentThread]);
//            }
//            });
//        dispatch_sync(queue, ^{
//            for (int i = 0; i < 2; ++i) {
//                NSLog(@"3------%@",[NSThread currentThread]);
//            }
//            });
print("syncSerial---end");
}

///执行完栅栏前面的操作之后,才执行栅栏操作,最后再执行栅栏后边的操作
func barrier() {
/* swift */
let queue = DispatchQueue(label: "com.lin.syc")
queue.async {
print("----1---",Thread.current)
}
queue.async {
print("----2---",Thread.current)
}
queue.async(group: nil, qos: .default, flags: .barrier) {
print("----3---",Thread.current)
}
queue.async {
print("----4---",Thread.current)
}
/* oc */
//        dispatch_queue_t queue = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT);
//
//        dispatch_async(queue, ^{
//            NSLog(@"----1-----%@", [NSThread currentThread]);
//            });
//        dispatch_async(queue, ^{
//            NSLog(@"----2-----%@", [NSThread currentThread]);
//            });
//
//        dispatch_barrier_async(queue, ^{
//            NSLog(@"----barrier-----%@", [NSThread currentThread]);
//            });
//
//        dispatch_async(queue, ^{
//            NSLog(@"----3-----%@", [NSThread currentThread]);
//            });
//        dispatch_async(queue, ^{
//            NSLog(@"----4-----%@", [NSThread currentThread]);
//            });
}

///分别异步执行2个耗时操作,然后当2个耗时操作都执行完毕后再回到主线程执行操作
func group() {
/* swift */
let group = DispatchGroup()
let queue = DispatchQueue(label: "com.lin.syc")
queue.async(group: group, qos: .default, flags: []) {
print("----1---",Thread.current)
}
queue.async(group: group, qos: .default, flags: []) {
print("----2---",Thread.current)
}
group.notify(queue: .main) {

}
/* oc */
//        dispatch_group_t group =  dispatch_group_create();
//
//        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            // 执行1个耗时的异步操作
//            });
//
//        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            // 执行1个耗时的异步操作
//            });
//
//        dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//            // 等前面的异步操作都执行完毕后,回到主线程...
//            });
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  object-c swift gcd