swift3.0+GCD小小的尝试

  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 gcd
个人分类: iOS开发笔记
上一篇iOS全局设置navigation的样式
下一篇swift内购
想对作者说点什么? 我来说一句

Flash小游戏——对对碰V3.0

2011年06月23日 162KB 下载

没有更多推荐了,返回首页

关闭
关闭