Code Monkey home page Code Monkey logo

swift-gcd's Introduction

Swift-GCD-Program

需要Objective-C版本GCD的请移步-Objective-C版本GCD

//
//  ViewController.swift
//  Swift-GCD
//
//  Created by YouXianMing on 2017/9/19.
//  Copyright © 2017年 TechCode. All rights reserved.
//
import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
    }
    
    private func gcdQueueUse() {
        
        // Excute in main queue.
        GCDQueue.Main.excute {
            
            print("GCDQueue.Main.excute")
        }
        
        // Excute in global queue.
        GCDQueue.Global().excute {
            
            print("GCDQueue.Global().excute")
        }
        
        // Excute in concurrent queue.
        GCDQueue.Concurrent().excute {
            
            GCDQueue.Global().excuteAndWaitsUntilTheBlockCompletes {
                
                print("🔥 01")
            }
            
            GCDQueue.Global().excuteAndWaitsUntilTheBlockCompletes {
                
                print("🔥 02")
            }
            
            GCDQueue.Global().excuteAndWaitsUntilTheBlockCompletes {
                
                print("🔥 03")
            }
            
            GCDQueue.Global().excuteAndWaitsUntilTheBlockCompletes {
                
                print("🔥 04")
            }
        }
        
        // GCDQueue excute in global queue after delay 2s.
        GCDQueue.Global().excuteAfterDelay(2) {
            
            print("GCDQueue.Global().excuteAfterDelay 2 Seconds")
        }
    }
    
    private func gcdSerialQueueUse() {
        
        let serialQueue = GCDQueue.Serial()
        
        serialQueue.excute {
            
            for i in 0..<10 {
                
                print("🔥" + String(i))
            }
        }
        
        serialQueue.excute {
            
            for i in 0..<10 {
                
                print("❄️" + String(i))
            }
        }
    }
    
    private func gcdConcurrentQueueUse() {
        
        let concurrentQueue = GCDQueue.Concurrent()
        
        concurrentQueue.excute {
            
            for i in 0..<10 {
                
                print("🔥" + String(i))
            }
        }
        
        concurrentQueue.excute {
            
            for i in 0..<10 {
                
                print("❄️" + String(i))
            }
        }
    }
    
    private func gcdGroupNormalUse() {
        
        // Init group.
        let group = GCDGroup()
        
        // Excute in group.
        GCDQueue.Global().excuteInGroup(group) {
            
            print("Do work A.")
        }
        
        // Excute in group.
        GCDQueue.Global().excuteInGroup(group) {
            
            print("Do work B.")
        }
        
        // Excute in group.
        GCDQueue.Global().excuteInGroup(group) {
            
            print("Do work C.")
        }
        
        // Excute in group.
        GCDQueue.Global().excuteInGroup(group) {
            
            print("Do work D.")
        }
        
        // Notify in queue by group.
        group.notifyIn(GCDQueue.Main) {
            
            print("Finish.")
        }
    }
    
    private func gcdGroupEnterAndLeaveUse() {
        
        // Init group.
        let group = GCDGroup()
        
        group.enter()
        group.enter()
        group.enter()
        
        print("Start.")
        
        GCDQueue.ExcuteInGlobalAfterDelay(3) {
            
            print("Do work A.")
            group.leave()
        }
        
        GCDQueue.ExcuteInGlobalAfterDelay(4) {
            
            print("Do work B.")
            group.leave()
        }
        
        GCDQueue.ExcuteInGlobalAfterDelay(2) {
            
            print("Do work C.")
            group.leave()
        }
        
        // Notify in queue by group.
        group.notifyIn(GCDQueue.Main) {
            
            print("Finish.")
        }
    }
    
    private func gcdGroupWaitUse() {
        
        // Init group.
        let group = GCDGroup()
        
        group.enter()
        group.enter()
        
        print("Start.")
        
        GCDQueue.ExcuteInGlobalAfterDelay(3) {
            
            print("Do work A.")
            group.leave()
        }
        
        GCDQueue.ExcuteInGlobalAfterDelay(5) {
            
            print("Do work B.")
            group.leave()
        }
        
        let waitSeconds = arc4random() % 2 == 0 ? 4 : 6
        print("wait \(waitSeconds) seconds.")
        print(group.waitForSeconds(seconds: Float(waitSeconds)))
        print("wait finish.")
        
        // Notify in queue by group.
        group.notifyIn(GCDQueue.Main) {
            
            print("Finish.")
        }
    }

    
    private func gcdSemaphoreWaitForeverUse() {
        
        // Init semaphore.
        let semaphore = GCDSemaphore()
        
        print("start.")
        
        GCDQueue.Global().excute {
            
            semaphore.wait()
            print("Done 1")
            
            semaphore.wait()
            print("Done 2")
        }
        
        GCDQueue.Global().excuteAfterDelay(3) {
            
            semaphore.signal()
        }
        
        GCDQueue.Global().excuteAfterDelay(4) {
            
            semaphore.signal()
        }
    }
    
    private func gcdSemaphoreWaitSecondsUse() {
        
        // Init semaphore.
        let semaphore = GCDSemaphore()
        
        print("start.")
        
        GCDQueue.Global().excute {
            
            _ = semaphore.waitForSeconds(3)
            print("Done")
        }
        
        GCDQueue.Global().excuteAfterDelay(5) {
            
            print("signal")
            semaphore.signal()
        }
    }
    
    private func gcdTimerUse() {
        
        let gcdTimer = GCDTimer(in: GCDQueue.Global(), delay: 2, interval: 3)
        
        print("Start.")
        
        var count : Int = 0
        gcdTimer.setTimerEventHandler {_ in
            
            count += 1
            
            print("\(count)")
            
            if count == 5 {
                
                print("suspend")
                gcdTimer.suspend()
                
                GCDQueue.ExcuteInGlobalAfterDelay(2.0, {
                    
                    print("start")
                    gcdTimer.start()
                })
            }
            
            if count >= 10 {
                
                gcdTimer.destroy()
            }
        }
        
        gcdTimer.setDestroyEventHandler {
            
            print("Destroy event.")
        }
        
        gcdTimer.start()
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.