iOS development: Nsthread multi-threaded use

Source: Internet
Author: User

One: Create threads,nsthread Three ways to create threads commonly:

       //1: Create a startup manuallyLet Thread:nsthread = Nsthread (target:self, selector:"dosomething:",Object:"param") Thread.Name="ChildThread1" //How to manually create a thread you can set the name of a threadThread.Start ()//2: Create complete auto startNsthread.detachnewthreadselector ("dosomething:", Totarget:self, Withobject:"param")        //3 Creating an automatic start implicitlySelf.performselectorinbackground ("dosomething:", Withobject:"param")        

Two: Nsthread common functions

     // 1. Get the main thread         nsthread.mainthread (        )//2. Get Current threads         Nsthread.currentthread ()        //3. Thread Sleep        Nsthread.sleepfortimeinterval (< #T # #ti : nstimeinterval# #NSTimeInterval #>)        nsthread.sleepuntildate (< #T # #date: nsdate# #NSDate #> )

Three: Thread synchronization and Thread safety

  Thread synchronization: Prevent multiple threads from seizing data security issues        caused by unified resources // Lock code blocks to ensure thread safety        lock: Nslock =  Nslock ()        lock. Lock ()        //something        lock. Unlock ()

Four: inter-thread communication

       //ensure thread safety, return to the main thread after the child thread finishes updating UI Waituntildone indicates whether to wait for completion, and if true, after the main thread completes the operation, execute the following codeSelf.performselectoronmainthread ("dosomething", Withobject:"param", Waituntildone:false)        //If you are updating only a single control, you can use the following simpler method, such as setting a pictureLet imageview:uiimageview=Uiimageview () imageview.performselectoronmainthread ("setimage:", Withobject:uiimage (), Waituntildone:false)

V: Thread-deferred execution

     // deferred Execution (note, deferred execution do not use thread sleep to implement, blocking the current thread)        Self.performselector ("dosomething:" "param"3)

Finish!

iOS development: Nsthread multi-threaded use

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.