3 ways to create a child thread with Nsthread
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. Get current thread//Nsthread *cthread = [NSThr EAD currentthread];//////2. Print thread//NSLog (@ "%@", cthread);/////3.h time-consuming operation//for (int i = 0; i < 999 9; i++) {//NSLog (@ "%@", cthread);//}////return null;//}-(ibaction) Btnonclick {//1. Get Current thread Nsthread * Cthread = [Nsthread CurrentThread]; Nsthread *MT = [Nsthread mainthread]; 2. Print thread NSLog (@ "%@", Cthread); NSLog (@ "%@", MT); 3. Perform a time-consuming operation: Create a set of sub-threads [self threadCreate3]; }-(void) Run: (NSString *) parma{//[nsthread threadpriority];////[Nsthread setthreadpriority:0.55]; Value 0-1, default 0.5 for (int i = 0; i < 9999; i++) {NSLog (@ "%@---------%@", [nsthRead CurrentThread], Parma); }}-(void) threadcreate5{//detached sub-thread [Nsthread detachnewthreadselector: @selector (run:) totarget:self withobject:@ "222 2222 "];} -(void) threadcreate4{//separated sub-thread [Nsthread detachnewthreadselector: @selector (run:) totarget:self withobject:@ "22222 22 "];} /** * Nsthread Creation Method 3: Hidden thread creation, and direct (auto) start */-(void) threadcreate3{[self performselectorinbackground: @selector (run:) with object:@ "333333"];} /** * Creation Method 2: Auto start */-(void) threadcreate2{//detached sub-thread after creating thread [Nsthread detachnewthreadselector: @selector (run:) totarg Et:self withobject:@ "2222222"];} /** * Creation Method 1:① first creates the initialization child thread ② restart */-(void) threadcreate{nsthread *thread1 = [[Nsthread alloc] initwithtarget:self selector: @selector (run:) object:@ "Heheh"]; Thread1.name = @ "Thread1"; Open thread [thread1 start]; Nsthread *thread2 = [[Nsthread alloc] initwithtarget:self selector: @selector (run:) object:@ "Heheh"]; Thread2.name = @ "Thread2"; Open thread [thread2 start]; Nsthread *thread3 = [[NSthread Alloc] initwithtarget:self selector: @selector (run:) object:@ "Heheh"]; Thread3.name = @ "33"; Open thread [thread3 start];} @end
Using Nsthread is not commonly used in development, so be aware of it