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.