美文网首页
9.6、GCD多线程

9.6、GCD多线程

作者: 艾希_可可 | 来源:发表于2018-06-27 13:25 被阅读28次

import UIKit

class ViewController: UIViewController {
var headImage = UIImageView()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.createGCDBtn()
    let imageV = UIImageView(frame: CGRect(x: 80, y: 580, width: 100, height: 120))
    headImage = imageV
    self.view.addSubview(imageV)
    imageV.backgroundColor = UIColor.brown
}
//创建2个按钮来点击GCD任务
func createGCDBtn() {
    let btn = UIButton(frame: CGRect(x: 100, y: 100, width: 200, height: 40))
    self.view.addSubview(btn)
    btn.backgroundColor = UIColor.blue
    btn.setTitle("global Queue + 异步", for: .normal)
    btn.addTarget(self, action: #selector(testglobalQueue), for: .touchUpInside)
    
    let btn2 = UIButton(frame: CGRect(x: 100, y: 200, width: 200, height: 40))
    self.view.addSubview(btn2)
    btn2.backgroundColor = UIColor.blue
    btn2.addTarget(self, action: #selector(testMainQueue), for: .touchUpInside)
    btn2.setTitle("main Queue + 异步", for: .normal)

    let btn3 = UIButton(frame: CGRect(x: 100, y: 300, width: 200, height: 40))
    btn3.setTitle("加载图片", for: .normal)
    self.view.addSubview(btn3)
    btn3.backgroundColor = UIColor.blue
    btn3.addTarget(self, action: #selector(requestImages), for: .touchUpInside)
    
    let btn4 = UIButton(frame: CGRect(x: 100, y: 400, width: 200, height: 40))
    self.view.addSubview(btn4)
    btn4.backgroundColor = UIColor.blue
    btn4.setTitle("队列", for: .normal)
    btn4.addTarget(self, action: #selector(队列方法), for: .touchUpInside)
}
//    什么是GCD?
//     (GCD) 是Apple开发的一个多核编程的较新的解决方法。它主要用于优化应用程序以支持多核处理器以及其他对称多处理系统。它是一个在线程池模式的基础上执行的并行任务。
//    1.1 任务的分类:同步、异步。
//    异步(asynchronous)具备开启新线程的能力,也具备跳过当前代码继续往下执行的能力。
//    同步(synchronous)不具备开启新线程的能力,也不具备跳过当前代码继续往下执行的能力。
//    换句话简单的说,异步任务就是可以同时开启多个跑道,同时跑好多辆车。同步就是只有一条车道,堵死也飞不过去,只能乖乖的等着,一辆接一辆。
// 同步执行任务创建方法
//    dispatch_sync(queue, ^{
//    NSLog(@"%@",[NSThread currentThread]);    // 这里放任务代码
//    });
// 异步执行任务创建方法
//    dispatch_async(queue, ^{
//    NSLog(@"%@",[NSThread currentThread]);    // 这里放任务代码
//    });

//    1.2 队列的分类
//    有两种:串行队列(Serial Dispatch Queue)、并发队列(Concurrent Dispatch Queue)。
//    串行队列(Serial Dispatch Queue):
//    让任务一个接着一个有序的执行,一个任务执行完毕后,再执行下一个任务。
//
//    并发队列(Concurrent Dispatch Queue)
//    可以让多个任务同时执行,自动开启多个线程同时执行多个任务。

//    主队列(main queue):是Serial Queue中特殊的一种。只能在主线程中进行,并且主队列里面的任务,只有当主线程空闲的时候才能被执行。用来刷新UI使用。
//    全局队列(global queue):是Concurrent Queue中特殊的一种。用来执行耗时操作。
//使用全局队列,开启异步任务

//    如果有多任务,工作中最最省事儿常用的就是global Queue + 异步。单任务、刷新UI就用main Queue + 异步。

// global Queue + 异步
@objc func testglobalQueue() {
let globalQueue = DispatchQueue.global()
for i in 0...10 {
globalQueue.async {
print("global Queue + 异步I am No.(i), current thread name is:(Thread.current)")
}
}
}
//使用主队列main Queue + 异步
@objc func testMainQueue() {
let mainQueue = DispatchQueue.main
for i in 0...10 {
mainQueue.async {
print("I am No.(i), current thread name is:(Thread.current)")
}
}
}
// 在当前UI动作之外,开启一个global Queue+异步,用来下载图片。因为过程可能很耗时。
// 等下载完成后,开启一个main Queue+异步,把下载的图片赋值,刷新UI
@objc func requestImages() {
DispatchQueue.global().async {
if let url = URL.init(string: "https://placebeard.it/355/140"){
do {
let imageData = try Data(contentsOf:url)
let images = UIImage(data:imageData)

                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.seconds(2), execute: {
                    
                    //                    })
                    
                    //                    DispatchQueue.main.async {//直接使用主队列刷新
                    self.headImage.image = images
                    self.headImage.sizeToFit()
                })
            } catch {
                print(error)
            }
        }
        
    }
    
}
@objc func 队列方法() {
    //    6. GCD的其他方法
    //    1. GCD的栅栏方法 dispatch_barrier_async
    //    我们有时需要异步执行两组操作,而且第一组操作执行完之后,才能开始执行第二组操作。这样我们就需要一个相当于栅栏一样的一个方法将两组异步执行的操作组给分割起来,当然这里的操作组里可以包含一个或多个任务。这就需要用到dispatch_barrier_async方法在两个操作组间形成栅栏。
    let groups = DispatchGroup()
    groups.enter()
    self.twoTask()
    groups.leave()
    groups.enter()
    self.oneTask()
    groups.leave()
    groups.notify(queue: DispatchQueue.main) {
        print("end")
    }
}
func oneTask() {
    for i in 0...10 {
        print(i)
    }
    print("-------------one----------")
}
func twoTask() {
    for i in 0...150 {
        print("-----&&&\(i)")
    }
    print("******two*****")
}
func threeTask() {
    print("three")
}
func fourTask() {
    print("four")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

相关文章

网友评论

      本文标题:9.6、GCD多线程

      本文链接:https://www.haomeiwen.com/subject/vwubyftx.html