swift3.0+GCD小小的尝试

原创 2016年08月29日 19:32:29
  1. 线程同步+串行队列
        let queue = DispatchQueue.init(label: "myQueue")
        queue.sync {

            print("-----1  begin \(Thread.current)")

            print("-----1  end \(Thread.current)")
        }
        print("++++++5 \(Thread.current)")
        queue.sync {
            print("-----2  begin \(Thread.current)")

            print("-----2  end \(Thread.current)")
        }
        print("++++++6 \(Thread.current)")
        queue.sync {
            print("-----3  begin \(Thread.current)")

            print("-----3  end \(Thread.current)")
        }
        print("++++++7 \(Thread.current)")
        queue.sync {
            print("-----4  begin \(Thread.current)")

            print("-----4  end \(Thread.current)")
        }
        print("++++++8 \(Thread.current)")

执行结果

-----1  begin <NSThread: 0x60000007d580>{number = 1, name = main}
-----1  end <NSThread: 0x60000007d580>{number = 1, name = main}
++++++5 <NSThread: 0x60000007d580>{number = 1, name = main}
-----2  begin <NSThread: 0x60000007d580>{number = 1, name = main}
-----2  end <NSThread: 0x60000007d580>{number = 1, name = main}
++++++6 <NSThread: 0x60000007d580>{number = 1, name = main}
-----3  begin <NSThread: 0x60000007d580>{number = 1, name = main}
-----3  end <NSThread: 0x60000007d580>{number = 1, name = main}
++++++7 <NSThread: 0x60000007d580>{number = 1, name = main}
-----4  begin <NSThread: 0x60000007d580>{number = 1, name = main}
-----4  end <NSThread: 0x60000007d580>{number = 1, name = main}
++++++8 <NSThread: 0x60000007d580>{number = 1, name = main}
++++++8

和不调用线程直接在代码块中写效果相同, 在主线程中执行,不新建线程
2. 线程同步+并行队列

        let queue = DispatchQueue.init(label: "my_queue", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)
        queue.sync {

            print("-----1  begin \(Thread.current)")

            print("-----1  end \(Thread.current)")
        }
        print("++++++5 \(Thread.current)")
        queue.sync {
            print("-----2  begin \(Thread.current)")

            print("-----2  end \(Thread.current)")
        }
        print("++++++6 \(Thread.current)")
        queue.sync {
            print("-----3  begin \(Thread.current)")

            print("-----3  end \(Thread.current)")
        }
        print("++++++7 \(Thread.current)")
        queue.sync {
            print("-----4  begin \(Thread.current)")

            print("-----4  end \(Thread.current)")
        }
        print("++++++8 \(Thread.current)")
-----1  begin <NSThread: 0x6100000770c0>{number = 1, name = main}
-----1  end <NSThread: 0x6100000770c0>{number = 1, name = main}
++++++5 <NSThread: 0x6100000770c0>{number = 1, name = main}
-----2  begin <NSThread: 0x6100000770c0>{number = 1, name = main}
-----2  end <NSThread: 0x6100000770c0>{number = 1, name = main}
++++++6 <NSThread: 0x6100000770c0>{number = 1, name = main}
-----3  begin <NSThread: 0x6100000770c0>{number = 1, name = main}
-----3  end <NSThread: 0x6100000770c0>{number = 1, name = main}
++++++7 <NSThread: 0x6100000770c0>{number = 1, name = main}
-----4  begin <NSThread: 0x6100000770c0>{number = 1, name = main}
-----4  end <NSThread: 0x6100000770c0>{number = 1, name = main}
++++++8 <NSThread: 0x6100000770c0>{number = 1, name = main}

队列中的函数还是顺序执行,不会新建线程
3. 线程异步+串行队列

        let queue = DispatchQueue.init(label: "myQueue")
        queue.async {

            print("-----1  begin \(Thread.current)")

            print("-----1  end \(Thread.current)")
        }
        print("++++++5 \(Thread.current)")
        queue.async {
            print("-----2  begin \(Thread.current)")

            print("-----2  end \(Thread.current)")
        }
        print("++++++6 \(Thread.current)")
        queue.async {
            print("-----3  begin \(Thread.current)")

            print("-----3  end \(Thread.current)")
        }
        print("++++++7 \(Thread.current)")
        queue.async {
            print("-----4  begin \(Thread.current)")

            print("-----4  end \(Thread.current)")
        }
        print("++++++8 \(Thread.current)")
++++++5 <NSThread: 0x618000073c40>{number = 1, name = main}
-----1  begin <NSThread: 0x60000007c4c0>{number = 3, name = (null)}
-----1  end <NSThread: 0x60000007c4c0>{number = 3, name = (null)}
++++++6 <NSThread: 0x618000073c40>{number = 1, name = main}
-----2  begin <NSThread: 0x60000007c4c0>{number = 3, name = (null)}
-----2  end <NSThread: 0x60000007c4c0>{number = 3, name = (null)}
++++++7 <NSThread: 0x618000073c40>{number = 1, name = main}
-----3  begin <NSThread: 0x60000007c4c0>{number = 3, name = (null)}
++++++8 <NSThread: 0x618000073c40>{number = 1, name = main}
-----3  end <NSThread: 0x60000007c4c0>{number = 3, name = (null)}
-----4  begin <NSThread: 0x60000007c4c0>{number = 3, name = (null)}
-----4  end <NSThread: 0x60000007c4c0>{number = 3, name = (null)}

队列中的人物顺序执行,但是会新建线程和主线程异步执行
4. 线程异步+并行队列

        let queue = DispatchQueue.init(label: "my_queue", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)
        queue.async {

            print("-----1  begin \(Thread.current)")

            print("-----1  end \(Thread.current)")
        }
        print("++++++5 \(Thread.current)")
        queue.async {
            print("-----2  begin \(Thread.current)")

            print("-----2  end \(Thread.current)")
        }
        print("++++++6 \(Thread.current)")
        queue.async {
            print("-----3  begin \(Thread.current)")

            print("-----3  end \(Thread.current)")
        }
        print("++++++7 \(Thread.current)")
        queue.async {
            print("-----4  begin \(Thread.current)")

            print("-----4  end \(Thread.current)")
        }
        print("++++++8 \(Thread.current)")
-----1  begin <NSThread: 0x60000006c2c0>{number = 3, name = (null)}
++++++5 <NSThread: 0x600000067e80>{number = 1, name = main}
-----1  end <NSThread: 0x60000006c2c0>{number = 3, name = (null)}
-----2  begin <NSThread: 0x60000006c2c0>{number = 3, name = (null)}
-----2  end <NSThread: 0x60000006c2c0>{number = 3, name = (null)}
++++++6 <NSThread: 0x600000067e80>{number = 1, name = main}
++++++7 <NSThread: 0x600000067e80>{number = 1, name = main}
++++++8 <NSThread: 0x600000067e80>{number = 1, name = main}
-----3  begin <NSThread: 0x60000006c2c0>{number = 3, name = (null)}
-----4  begin <NSThread: 0x60000006cb40>{number = 4, name = (null)}
-----3  end <NSThread: 0x60000006c2c0>{number = 3, name = (null)}
-----4  end <NSThread: 0x60000006cb40>{number = 4, name = (null)}

会新建多条线程,任务会异步的开始执行

swift 3.0 闭包,项目使用例子,结合GCD

国庆长假,不想一行白鹭上青天,回家挤在在最中间。就把征战已久的古董pro挖出,硬生生升到了macOS Sierrra,跑起了 Xcode 8 ,写起了swift。 先来一波实用干货。 swift...
  • lkycan
  • lkycan
  • 2017年03月14日 10:14
  • 464

Swift3.0 GCD定时器的使用,实现倒计时,UIDatePicker的使用, 仿写一个活动倒计时的DEMO

Swift3.0 GCD定时器实现倒计时,UIDatePicker的使用, 仿写一个活动倒计时的DEMO

Swift3.0学习之GCD

昨天开始学习了swift3.0这门语言,发现它和c、c++、Object-c、java有很多相同的地方,她很好地融入了以上4种语言,并且它可以用SQS来形容,simply、qucikly、stably...

Swift3.0 gcd学习(3)

Swift3.0 gcd学习笔记(3)Deadlock 死锁之前提到过gcd死锁:两个线程因为互相等待对方完成而都被卡住。第一个不能完成是因为它在等待第二个的完成。但第二个也不能完成,因为它在等待第一...

Swift3.0 gcd学习(1)

Swift3.0 gcd学习(1)最近机缘巧合花了点时间研究了下IOS的多线程,这里做个总结,算是学习笔记,不足之处,还请大家多指正。废话不多,这就开始! demo git地址多线程概述IOS多线程...

swift 3.0 GCD使用

swift 3中对C层级的GCD的API进行了彻头彻尾的改变。本文将从实际使用场景来了解一下新的api使用。 dispatch_async 一个常见的场景就是在一个全局队列进行一些操作后切换到主线...

Swift3.0 GCD多线程详解

iOS10+ Swift3.0 DispatchQueue DispatchGroup 多线程

Swift 3.0中GCD的常用方法

随着苹果推出swift3.0,很多API都发生了变化,下面我就来总结下GCD的一些常用API用法。 首先为了方便先定义几个属性: class ViewController:UIViewContro...
  • hmh007
  • hmh007
  • 2016年10月28日 10:24
  • 4516

Swift3.0 gcd学习(2)

Swift3.0 gcd学习(2) 上一篇简单梳理了下gcd的基础概念和一些基本的使用方法。这一篇希望再深入研究下gcd的一些玩法,有错误希望大家指正。 barrier 有时会在一个并发的队列里...

Swift-GCD的基本使用(Swift 3.0)

在 Swift 3.0 以后, GCD 的使用发生了很大的变化, 相比 Swift 2.0+ ,变得更加简洁了!首先学习几个比较重要的概念:队列 1 - 并发队列可以让多个任务并发(同时)执行(自动开...
  • Mazy_ma
  • Mazy_ma
  • 2016年12月05日 13:58
  • 1740
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:swift3.0+GCD小小的尝试
举报原因:
原因补充:

(最多只允许输入30个字)