iOS multithreaded Development Small Demo2,nsthread article

Source: Internet
Author: User

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

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.