iOS Multithreaded Development Small Demo

Source: Internet
Author: User

First, demonstrate the blocking of the main thread

  dyfviewcontroller.m//  623-01-blocking multithreading////  Created by dyf on 14-6-23.//  Copyright (c) 2014 ___ fullusername___. All rights reserved.//#import "DYFViewController.h" @interface Dyfviewcontroller () @end @implementation dyfviewcontroller-(ibaction) Btnonclick {    //1. Gets the current thread    nsthread *thread = [Nsthread CurrentThread];        2. Print thread    NSLog (@ "%@", thread);        3. Perform a time-consuming operation for    (int i = 0; i < 9999; i++) {        NSLog (@ "%@", thread);        At this point, click the button, before the time-consuming operation, the button has been highlighted, during the user to click Other UI control also bad response    }}-(void) viewdidload{    [Super viewdidload];//do any Additional setup after loading the view, typically from a nib.} -(void) didreceivememorywarning{    [Super didreceivememorywarning];    Dispose of any resources the can be recreated.} @end

It is easy to find that during the time-consuming operation, other UI actions are delayed, causing the user to lag

--------Create a child threading method 1--pthread

  dyfviewcontroller.m//  623-02-pthread////  Created by dyf on 14-6-23.//  Copyright (c) 2014 ___ fullusername___. All rights reserved.//#import "DYFViewController.h" #import <pthread.h> @interface Dyfviewcontroller () @ End@implementation dyfviewcontroller//C language function void *run (void *data) {    //1. Gets the current thread    nsthread *cthread = [Nsthread CurrentThread];        2. Print thread    NSLog (@ "%@", cthread);        3.h time-consuming operation    for (int i = 0; i < 9999; i++) {        NSLog (@ "%@", Cthread);    }    return NULL;} -(Ibaction) Btnonclick {    //1. Gets the current thread    nsthread *thread = [Nsthread CurrentThread];        2. Print thread    NSLog (@ "%@", thread);        3. Perform a time-consuming operation: Create a set of sub-threads        pthread_t threadId;    Pthread_create (&threadid, NULL, *run, NULL);   } @end

Summary: This method is not commonly used, understand can

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.