Use nsthread to start a thread

Source: Internet
Author: User

Nsthread is the main character of a thread. We use this class most of the time.

Because my Macbook is temporarily unavailable, only the code is posted here:

Create a view-based application.

In viewcontroller. H

@ Interface tnsthreadviewcontroller: uiviewcontroller {
Nsthread * T1;
Bool bexit;
}
@ Property bool bexit;
-(Ibaction) checkit :( ID) sender;
-(Ibaction) start2 :( ID) sender;
-(Ibaction) Start :( ID) sender;
-(Void) dosomething :( ID) data;
@ End

 

In viewcontroller. m

 

# Import "tnsthreadviewcontroller. H"

@ Implementation tnsthreadviewcontroller
@ Synthesize bexit;
-(Void) dosomething :( ID) data
{

/* [T1 threaddictionary] setobject: @ "KKK" forkey: @ "K11"];
Nsmutabledictionary * MD = [T1 threaddictionary];
Nsstring * t = [MD objectforkey: @ "K11"];
Nslog (@ "% @", t );
Nslog (@ "kasdkf ");*/
// Nsstring * D = (nsstring *) data;
// Nslog (@ "% @", d );
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
Nsstring * s = [nsstring stringwithformat: @ "% d", 1];
Nslog (@ "% @", S );
While (true ){
[Nsthread sleepfortimeinterval: 0.5];
Nslog (@ "in thread ");
Tnsthreadviewcontroller * inputdata = (tnsthreadviewcontroller *) data;
If (inputdata. bexit)
Break;
}
[Pool release];


}
-(Void) dealloc
{
[Super dealloc];
}

-(Void) didreceivememorywarning
{
// Releases the view if it doesn't have a superview.
[Super didreceivememorywarning];

// Release any cached data, images, etc that aren't in use.
}

# Pragma mark-view Lifecycle

// Implement viewdidload to do additional setup after loading the view, typically from a nib.
-(Void) viewdidload
{
Bexit = no;
[Super viewdidload];
}

-(Void) viewdidunload
{
[Super viewdidunload];
// Release any retained subviews of the main view.
// E.g. Self. myoutlet = nil;
}

-(Bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation
{
// Return YES For supported orientations
Return (interfaceorientation = uiinterfaceorientationportrait );
}

-(Ibaction) checkit :( ID) sender {
Bexit = yes;
If ([nsthread ismultithreaded]) {
Nslog (@ "It is Multi thread ");
}
Else {
Nslog (@ "it is not multi thread ");
}
}

-(Ibaction) start2 :( ID) sender {
If (T1! = Nil)
{

[T1 release];
T1 = nil;
}
T1 = [[nsthread alloc] initwithtarget: Self selector: @ selector (dosomething :) object: Self];
[T1 setstacksize: 1024*1024];
[T1 setthreadpriority: 0.5];

[T1 start];

/* Nsthread * t2 = [[nsthread alloc] initwithtarget: Self selector: @ selector (dosomething :) object: @ "2"];
[T2 setstacksize: 1024*1024];
[T2 setthreadpriority: 0.9];

[T2 start]; */

}

-(Ibaction) Start :( ID) sender {
// [Self defined mselectorinbackground: <# (SEL) #> withobject: <# (ID) #>
[Nsthread detachnewthreadselector: @ selector (dosomething :) totarget: Self withobject: Nil];
}
@ End

 

 

It should be noted that the stack size of the main thread of the IOS application is 1 MB by default, and the stack size of other threads is 512 K by default. Many online articles have said that this size cannot be adjusted, that is to upload and adjust the size. Please refer to my code.

 

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.