Swift Basic syntax-multithreading

Source: Internet
Author: User
Tags gcd

Ways to achieve multithreading and benefits:

1,Nsthread

Lightweight is the lightest, but requires itself to manage the thread's lifecycle and thread synchronization. Thread synchronization locks the data to a certain amount of system consumption.

2.Cocoa nsopertion (nsopertion and Nsopertionquene)

Cocoa Nsopertion does not need to care about thread management and data synchronization. The related classes are nsopertion and Nsopertionquene. Startup Nsopertion is an abstract class that uses its subclasses to implement it or the two subclasses it defines: Nsinvocationopertion and nsblockoperation. Create an object of the Nsopertion subclass and add the object to the Nsopertionquene queue to execute

3.Grand Central Dispatch (GCD)

GCD is a multi-core programming solution developed by Apple. The Mac OS x10.6 Snow Leopard was first launched and then introduced into the ios4.0. GCD is an efficient and powerful technology to replace Nsthread, Nsoperationqueue, nsinvocationoperation and other technologies. It is the most abstract and simple to use, and is the most recommended for Apple.


---well, the crap is finally done.

1. Nsthread Example

1.1 via Integrated Nsthread

  main.swift//  swift_lession1////  Created by shaoyongyang  on 15/5/24.//  copyright  (c)  2015 year  shaoyongyang. All rights  reserved.//import foundationclass bigdog:nsthread {         func hello ()  {        // Nsthread.detachnewthreadselector ("Otherthread",  totarget: self, withobject: nil)          var mythread:nsthread = nsthread (Target: self,  selector:  "Otherthread",  object: nil)          Mythread.start ()         println (Nsthread.currentthread ())      }        func otherthread ()  {         println (Nsthread.currentthread())         println ("Hello! i am other thread ...")     }}var dog = bigdog () println (Dog.hello ()) for var i=0;i< 100000;i++ {    }
Nsthread.detachnewthreadselector ("Otherthread", Totarget:self, Withobject:nil)//Do not need start, directly execute function

2. Cocoa Nsoperation Example

3. Grand Central Dispatch Example


Swift Basic syntax-multithreading

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.