Simple use of swift GCD

  • Back to the main thread
        {DispatchQueue.main.async 
           // Print ( "here is the main thread") 
        }

 

  • Delay execution
        DEADLINE = DispatchTime.now the let () + 5.0 
        DispatchQueue.global () asyncAfter. (DEADLINE: DEADLINE) { 
            // Print ( "here do delay") 
        }
  • Open an asynchronous thread
        {DispatchQueue.main.async 
            Print ( "open a new asynchronous thread of execution") 
        }

 

  •  Open a thread synchronization
DispatchQueue.global().sync {
            
}
  •  Thread priority
        Queue var: DispatchQueue DispatchQueue.global = () // 
        //DispatchQueue.GlobalQueuePriority selected. There are 4 High, `default`, Low, background 
        Queue = DispatchQueue.global (priority: DispatchQueue.GlobalQueuePriority.background) the background // 
        // asynchronous task execution queue 
        queue.async { 
            // Print ( "open a new thread execution") 
        }

 

  •  Packet execution
       Group = DispatchGroup the let () 
        Queue = DispatchQueue.global (priority: DispatchQueue.GlobalQueuePriority.default) // perform default priority 
        for I in 0 .. <10 { 
            // asynchronous task execution queue 
            queue.async (group: group, execute : { 
                Print ( "queue.async (Group: Group \ (I)") 
            }) 
        } 
        // packet queue after execution completes 
        group.notify (queue: queue) { 
            Print ( "dispatch_group_notify") 
        }

 

  • Serial queue: only one thread is added to the operation in the queue by sequentially adding sequentially performed.
       serialQueue = DispatchQueue the let (label: "yangj", Attributes: []) 
        for I in 0 .. <10 { 
            // asynchronous task execution queue 
            serialQueue.async { 
                Print ( "serialQueue.async \ (I)") 
            } 
        }

 

  • Concurrent queue: There are multiple threads, it will come after the operation of these arrangements in the queue on the available processors, while ensuring advanced to the task priority.
        globalQueue = DispatchQueue.global the let (priority: DispatchQueue.GlobalQueuePriority.default) 
        for I in 0 .. <10 { 
            // asynchronous task execution queue 
            globalQueue.async { 
                Print ( "globalQueue.async \ (I)") 
            } 
        }
  •  signal
       // DispatchSemaphore because the signal magnitude is 1, you can only perform one 
        the let semaphore DispatchSemaphore = (value: 1) 
        the let = DispatchQueue.global Queue () 
        queue.async { 
            semaphore.wait () 
            the let DEADLINE = DispatchTime.now () 3.0 + 
            DispatchQueue.global () asyncAfter. (DEADLINE: DEADLINE) { 
                Print ( "-----------------. 1"); 
                semaphore.signal () 
            } 
            
        } 
        queue.async { 
            semaphore.wait () 
            the let DispatchTime.now DEADLINE = () + 10.0 
            DispatchQueue.global () asyncAfter. (DEADLINE: DEADLINE) {  
                 Print ( "2 -----------------" );
                 semaphore.signal () 
            }
            
        }
        queue.async {
            semaphore.wait()
            let deadline = DispatchTime.now() + 2.0
            DispatchQueue.global().asyncAfter(deadline: deadline) {
                print("-----------------3");
                semaphore.signal()
            }
            
        }

 

  • oc use in dispatch_group_enter
        // create a schedule set 
        the let workingGroup DispatchGroup = () 
        // create multiple columns 
        the let workingQueue = DispatchQueue (label: "request_queue") 
        
        // emulate asynchronous network request A 
        @ enrolled 
        workingGroup.enter () 
        workingQueue.async { 
            the Thread. SLEEP (forTimeInterval:. 1) 
            Print ( "Port A data request is completed") 
            // set out 
            workingGroup.leave () 
        } 
        
        // emulate asynchronous network request B 
        @ enrolled 
        workingGroup.enter () 
        workingQueue.async { 
            the Thread.sleep (forTimeInterval:. 1) 
            Print ( "port B data request completed")  
            // set out
            workingGroup.leave ()
        } 
        
        Print ( ", after performing operation in the asynchronous print execution beginning I") 
        
        // scheduling the group of tasks were completed 
        workingGroup.notify (Queue: workingQueue) { 
            Print ( "port A and port B data request have been completed!, begin to merge data from two interfaces ") 
        }

 

Guess you like

Origin www.cnblogs.com/hualuoshuijia/p/11699742.html
gcd