What are the multiple threads of the IOS face question?

Source: Internet
Author: User

Interviewer: You tell me about multithreading.

I: Multithreading, is multithreaded Bai ...

If all this answer, how many people want you.

=====================================================

First, we analyze the usage environment of multithreading:

Multithreading includes multi-threaded access to core data, parallel drawing of the UI, asynchronous network requests, and some scenarios for handling large files in the case of running-state memory crunch.

Secondly, the implementation of multithreading provided by iOS is illustrated separately.

Multi-threaded implementations in the following sets are available in iOS

1.Nsobjcet Implementation
The simplest multi-threaded execution mode//parameter 1: Need to execute the method in the background (child thread)//Parameter 2: Give this method a parameter [self performselectorinbackground: @selector (btnupclicked:) WI Thobject:nil];
2.NSThread implementations
Pros: In all multithreaded implementations the most lightweight//can be arbitrarily controlled according to the requirements of the thread object is: the locking of threads and other operations//disadvantage: control too cumbersome to control thread safety//Nsthread an object represents a thread Nsthr    EAD *thread = [[Nsthread alloc]initwithtarget:selfselector: @selector (btnupclicked:) Object:nil];    [Thread start]; [Thread release];
3.NSOperation implementations

 
@implementation myoperation-(void) main{//operation write the task to perform in the main method static int count = 0;    for (int i = 0; i < 600000000; i + +) {count + +;        } NSLog (@ "%d", count);    Determines whether the current task is the main thread 0 is not 1 for is BOOL b = [Nsthread ismainthread];        NSLog (@ "%d", b);    Nsthread *thread = [Nsthread CurrentThread];    NSLog (@ "Current thread:%@", thread); } @end
4.NSOperationQueue
    //  Task Queue (nsoperationqueue)   Internal management of some column threads          MyOperation *op1 = [[MyOperation alloc] init];     Myoperation *op2 = [[myoperation alloc] init];    myoperation  *op3 = [[myoperation alloc] init];    myoperation *op4  = [[MyOperation alloc] init];         nsoperationqueue *queue = [[nsoperationqueue alloc] init];         //  set the maximum number of concurrency     [queue setMaxConcurrentOperationCount:2];         [queue addoperation:op1];    [queue  addoperation:op2];    [queue addoperation:op3];    [queue  addoperation:op4];        //  Advantages: Ability to manage   thread safety   Reasonable scheduling of threads   lower system overhead   increased utilization of threads      //  Cons: Poor readability   writing is cumbersome
5.GCD
GCD is a queue-based approach to multi-threaded implementations//queue type//1. Parallel: Multiple tasks simultaneously execute Concurrent//2. Serial: Perform tasks sequentially (one task only) Serial//Create a team yourself column//parameter 1: Give the queue a name//Parameter 2: Select the type of queue dispatch_queue_concurrent (serial) dispatch_queue_attr_t queue = Dispatch_queue_creat        E ("Test", dispatch_queue_concurrent);    Use queue//parameter 1: in which queue executes code//Parameter 2: Code to execute Dispatch_async (queue, ^{[self btnupclicked:nil]; });
Advanced usage:
    //  system defines 5 queues     // 1.  one serial queue    home row action   :  managing the related transactions of the main thread         //  getting the home row     dispatch _queue_t mainqueue = dispatch_get_main_queue ();    //  perform tasks in the main queue//     dispatch_async (mainqueue, ^{//        [self  btnupclicked:nil];//    });             // 2. 4 Parallel queue   (parallel queue)     //  parameter 1:  Select the priority of a queue      //  parameter 2:  for future use   general fill 0    dispatch_queue_t globalqueue  = dispatch_get_global_queue (dispatch_queue_priority_default, 0);//     Dispatch_async (globalqueue, ^{//        [self btnupclicked: nil];//    });         //  high-end use     // 1. Global Queue Get Data   Back to the Home column load data into the view//    dispatch_async (globalqueue, ^{//        //        //  Global Queue (child thread) get data//         nsstring *str = @ "Htt://www.baidu.com";//         NSURL *url = [NSURL URLWithString:str];//         //        nsdata *data = [nsdata  datawithcontentsofurl:url];//        uiimage *image =  [UIImage imageWithData:data];//        //         //  return to main thread   load ui/update ui//         Dispatch_async (MAinqueue, ^{//           //             UIImageView *imageView = [[UIImageView  alloc] init];//            //  assigning values to views//             imageview.image = image;//             //         });//    });         // 2. A piece of code only executes     //  most of the time   is used in a singleton method     static dispatch_ Once_t oncetoken;    dispatch_once (&onceToken, ^{         nslog (@ "execute only");     });         // 3. A piece of code executes multiple times   (globalQueue valid)     //  parameter 1:  How many times     //  parameters 2:  in which queue is valid      //  parameter 3:  What to do     dispatch_apply (4, globalqueue, ^ ( Size_t a)  {        nslog (@ "%zu",  a);     });         // 4. Timing     //  Parameter 1:   interval seconds     //  parameter 2:  the content     dispatch_after (Dispatch_time ( dispatch_time_now,  (int64_t) (4 * nsec_per_sec)),  dispatch_get_main_queue (), ^{         nslog (@ "=====");     });


It is highly recommended to scare people with GCD. This is not what I said, they summed up, anyway good is a, especially encountered interviewer is not developed by iOS ... Zola pulled him back to GCD. A word gcd long live ~



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.