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