多线程GCD
分为:主队列、全局队列、自定义队列
dispatch_get_mian_queue
dispatch_get_global_queue
dispatch_queue_create
dispatch_async
dispatch_sync
dispatch_queue_concurrent
dispatch_queue_serial
在swift中队列的前缀均是是dispatch开头,如果创建一个队列,可以如下创建
let create_queue:dispatch_queue_t = dispatch_queue_create("a",DISPATCH_QUEUE_CONCURRENT)
let main:dispatch_queue_t = dispatch_get_main_queue()
let global_queue:dispatch_queue_t = dispatch_get_global_queue(0,0)
在创建全局队列时,有两个参数需要进行设置,第一个用于设置ID,标志queue,第二个用于设置队列的优先级,有四种用于选择
func dispatch_get_global_queue(identifier: Int, _ flags: UInt) -> dispatch_queue_t!
@param identifier
* A quality of service class defined in qos_class_t or a priority defined in
* dispatch_queue_priority_t.
*
* It is recommended to use quality of service class values to identify the
* well-known global concurrent queues:
* - QOS_CLASS_USER_INTERACTIVE
* - QOS_CLASS_USER_INITIATED
* - QOS_CLASS_DEFAULT
* - QOS_CLASS_UTILITY
* - QOS_CLASS_BACKGROUND
* The global concurrent queues may still be identified by their priority,
* which map to the following QOS classes:
* - DISPATCH_QUEUE_PRIORITY_HIGH: QOS_CLASS_USER_INITIATED
* - DISPATCH_QUEUE_PRIORITY_DEFAULT: QOS_CLASS_DEFAULT
* - DISPATCH_QUEUE_PRIORITY_LOW: QOS_CLASS_UTILITY
* - DISPATCH_QUEUE_PRIORITY_BACKGROUND: QOS_CLASS_BACKGROUND
用户可以自行创建队列,并设置该队列是串行队列还是并行队列
let create_queue:dispatch_queue_t = dispatch_queue_create("a",DISPATCH_QUEUE_CONCURRENT)
let create_queue:dispatch_queue_t = dispatch_queue_create("a",DISPATCH_QUEUE_SERAIL)
另外一种情况:
如果一个应用需要串行队列,并行任务,就需要设置代码
方案:设置并行队列,任务设置成同步,这样就使任务被串行执行 ,代码参考如下:
let create_queue:dispatch_queue_t = dispatch_queue_create("a",DISPATCH_QUEUE_CONCURRENT)
dispatch_sync(create_queue) { () -> Void in
for(var i:integer_t = 0; i < 5 ;i++){
print("create_queue1 ")
}
}
dispatch_sync(create_queue) { () -> Void in
for(var i:integer_t = 0; i < 5 ;i++){
print("create_queue2 ")
}
}
附录:整个GCD代码参考
//
// ViewController.swift
// GCD
//
// Created by WHJ on 16/1/18.
// Copyright © 2016年 WHJ. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
//多线程
var btn:UIButton?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
print("test.....")
btn = UIButton(frame: CGRect(x: 100, y: 100, width: 200, height: 40))
btn?.backgroundColor = UIColor.redColor()
btn?.setTitle("Click Me", forState: UIControlState.Normal)
btn?.addTarget(self, action: "testGCD:", forControlEvents: UIControlEvents.TouchUpInside)
// btn?.addTarget(<#T##target: AnyObject?##AnyObject?#>, action: Selector, forControlEvents: <#T##UIControlEvents#>)
self.view.addSubview(btn!)
}
func testGCD(sender:UIButton){
print("start test GCD")
//first create a queue task
/*GCD offers three kinds of queues
main : tasks execute serially on your application's main thread
concurrent: tasks are dequeued in FIFO order,but run concurrently and can finish in any order
serial:tasks execute one at a time in FIFO order
*/
//calling main_queue
// let main:dispatch_queue_t = dispatch_get_main_queue()
// dispatch_async(main) { () -> Void in
// for(var i:integer_t = 0; i < 5 ;i++){
// print("\(main) value : \(i)")
// }
// for(var j:integer_t = 0; j < 5 ;j++){
// print("\(main) value : \(j)")
// }
//
let global_queue:dispatch_queue_t = dispatch_get_global_queue(0,0)
// dispatch_async(global_queue) { () -> Void in
// for(var i:integer_t = 0; i < 5 ;i++){
// print("global_queue1 value : \(i)")
// }
//
// }
// dispatch_async(global_queue) { () -> Void in
// for(var i:integer_t = 0; i < 5 ;i++){
// print("global_queue2 value : \(i)")
// }
//
// }
//
// let create_queue:dispatch_queue_t = dispatch_queue_create("a",DISPATCH_QUEUE_SERIAL)
let create_queue:dispatch_queue_t = dispatch_queue_create("a",DISPATCH_QUEUE_CONCURRENT)
dispatch_async(create_queue) { () -> Void in
for(var i:integer_t = 0; i < 5 ;i++){
print("create_queue1 ")
}
}
dispatch_async(create_queue) { () -> Void in
for(var i:integer_t = 0; i < 5 ;i++){
print("create_queue2 ")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}