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