关闭

swift3.0+GCD小小的尝试

标签: swiftgcd
144人阅读 评论(0) 收藏 举报
分类:
  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)}

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4035次
    • 积分:53
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档