IOS development (67): A simple thread method

Source: Internet
Author: User

1 Preface

Use the NSObject instance method msmselectorinbackground: withObject: to create a thread without directly processing the thread.


2. code example
ZYAppDelegate. m

 

[Plain]
(Void) firstCounter {
@ Autoreleasepool {
NSUInteger counter = 0;
For (counter = 0; counter <10; counter ++ ){
NSLog (@ "First Counter = % lu", (unsigned long) counter );}
}
}
-(Void) secondCounter {
@ Autoreleasepool {
NSUInteger counter = 0;
For (counter = 0; counter <10; counter ++ ){
NSLog (@ "Second Counter = % lu", (unsigned long) counter );
}
}
}
-(Void) thirdCounter {
@ Autoreleasepool {
NSUInteger counter = 0;
For (counter = 0; counter <10; counter ++ ){
NSLog (@ "Third Counter = % lu", (unsigned long) counter );
}
}
}
 
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
// Call a method on a new background thread receiver.
[Self defined mselectorinbackground: @ selector (firstCounter) withObject: nil];
[Self defined mselectorinbackground: @ selector (secondCounter) withObject: nil];
[Self defined mselectorinbackground: @ selector (thirdCounter) withObject: nil];
Self. window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease];
// Override point for customization after application launch.
Self. viewController = [[[ZYViewController alloc] initWithNibName: @ "ZYViewController" bundle: nil] autorelease];
Self. window. rootViewController = self. viewController;
[Self. window makeKeyAndVisible];
Return YES;
}

-(Void) firstCounter {
@ Autoreleasepool {
NSUInteger counter = 0;
For (counter = 0; counter <10; counter ++ ){
NSLog (@ "First Counter = % lu", (unsigned long) counter );}
}
}
-(Void) secondCounter {
@ Autoreleasepool {
NSUInteger counter = 0;
For (counter = 0; counter <10; counter ++ ){
NSLog (@ "Second Counter = % lu", (unsigned long) counter );
}
}
}
-(Void) thirdCounter {
@ Autoreleasepool {
NSUInteger counter = 0;
For (counter = 0; counter <10; counter ++ ){
NSLog (@ "Third Counter = % lu", (unsigned long) counter );
}
}
}

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
// Call a method on a new background thread receiver.
[Self defined mselectorinbackground: @ selector (firstCounter) withObject: nil];
[Self defined mselectorinbackground: @ selector (secondCounter) withObject: nil];
[Self defined mselectorinbackground: @ selector (thirdCounter) withObject: nil];
Self. window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease];
// Override point for customization after application launch.
Self. viewController = [[[ZYViewController alloc] initWithNibName: @ "ZYViewController" bundle: nil] autorelease];
Self. window. rootViewController = self. viewController;
[Self. window makeKeyAndVisible];
Return YES;
}
Console result after running


21:37:23. 654 seconds mselectorinbackgroundtest [417: 3b03] Second Counter = 0

21:37:23. 654 export mselectorinbackgroundtest [417: 3903] First Counter = 0

21:37:23. 654 export mselectorinbackgroundtest [417: 4003] Third Counter = 0

21:37:23. 657 seconds mselectorinbackgroundtest [417: 3b03] Second Counter = 1

21:37:23. 657 export mselectorinbackgroundtest [417: 3903] First Counter = 1

21:37:23. 658 export mselectorinbackgroundtest [417: 4003] Third Counter = 1

21:37:23. 659 seconds mselectorinbackgroundtest [417: 3b03] Second Counter = 2

21:37:23. 659 export mselectorinbackgroundtest [417: 3903] First Counter = 2

21:37:23. 661 seconds mselectorinbackgroundtest [417: 3b03] Second Counter = 3

21:37:23. 663 export mselectorinbackgroundtest [417: 3903] First Counter = 3

21:37:23. 663 seconds mselectorinbackgroundtest [417: 3b03] Second Counter = 4

21:37:23. 659 export mselectorinbackgroundtest [417: 4003] Third Counter = 2

21:37:23. 664 export mselectorinbackgroundtest [417: 3903] First Counter = 4

21:37:23. 664 seconds mselectorinbackgroundtest [417: 3b03] Second Counter = 5

21:37:23. 665 export mselectorinbackgroundtest [417: 4003] Third Counter = 3

21:37:23. 667 export mselectorinbackgroundtest [417: 4003] Third Counter = 4

21:37:23. 667 export mselectorinbackgroundtest [417: 3903] First Counter = 5

21:37:23. 667 seconds mselectorinbackgroundtest [417: 3b03] Second Counter = 6

21:37:23. 668 export mselectorinbackgroundtest [417: 4003] Third Counter = 5

21:37:23. 669 export mselectorinbackgroundtest [417: 3903] First Counter = 6

21:37:23. 670 seconds mselectorinbackgroundtest [417: 3b03] Second Counter = 7

21:37:23. 670 export mselectorinbackgroundtest [417: 4003] Third Counter = 6

21:37:23. 671 seconds mselectorinbackgroundtest [417: 3b03] Second Counter = 8

21:37:23. 673 seconds mselectorinbackgroundtest [417: 3b03] Second Counter = 9

21:37:23. 673 export mselectorinbackgroundtest [417: 4003] Third Counter = 7

21:37:23. 671 export mselectorinbackgroundtest [417: 3903] First Counter = 7

21:37:23. 674 export mselectorinbackgroundtest [417: 4003] Third Counter = 8

21:37:23. 674 export mselectorinbackgroundtest [417: 3903] First Counter = 8

21:37:23. 675 export mselectorinbackgroundtest [417: 4003] Third Counter = 9

21:37:23. 675 export mselectorinbackgroundtest [417: 3903] First Counter = 9

 

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.