1 前言
使用 NSObject 的實例方法 performSelectorInBackground:withObject: ,來創建一個線程,而不需要直接處理線程。
2 代碼實例
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 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//在一個新的後台線程接收器上調用一個方法。
[self performSelectorInBackground:@selector(firstCounter) withObject:nil];
[self performSelectorInBackground:@selector(secondCounter) withObject:nil];
[self performSelectorInBackground:@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 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//在一個新的後台線程接收器上調用一個方法。
[self performSelectorInBackground:@selector(firstCounter) withObject:nil];
[self performSelectorInBackground:@selector(secondCounter) withObject:nil];
[self performSelectorInBackground:@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;
}
運行後控制台結果
2013-05-12 21:37:23.654 PerformSelectorInBackgroundTest[417:3b03] Second Counter = 0
2013-05-12 21:37:23.654 PerformSelectorInBackgroundTest[417:3903] First Counter = 0
2013-05-12 21:37:23.654 PerformSelectorInBackgroundTest[417:4003] Third Counter = 0
2013-05-12 21:37:23.657 PerformSelectorInBackgroundTest[417:3b03] Second Counter = 1
2013-05-12 21:37:23.657 PerformSelectorInBackgroundTest[417:3903] First Counter = 1
2013-05-12 21:37:23.658 PerformSelectorInBackgroundTest[417:4003] Third Counter = 1
2013-05-12 21:37:23.659 PerformSelectorInBackgroundTest[417:3b03] Second Counter = 2
2013-05-12 21:37:23.659 PerformSelectorInBackgroundTest[417:3903] First Counter = 2
2013-05-12 21:37:23.661 PerformSelectorInBackgroundTest[417:3b03] Second Counter = 3
2013-05-12 21:37:23.663 PerformSelectorInBackgroundTest[417:3903] First Counter = 3
2013-05-12 21:37:23.663 PerformSelectorInBackgroundTest[417:3b03] Second Counter = 4
2013-05-12 21:37:23.659 PerformSelectorInBackgroundTest[417:4003] Third Counter = 2
2013-05-12 21:37:23.664 PerformSelectorInBackgroundTest[417:3903] First Counter = 4
2013-05-12 21:37:23.664 PerformSelectorInBackgroundTest[417:3b03] Second Counter = 5
2013-05-12 21:37:23.665 PerformSelectorInBackgroundTest[417:4003] Third Counter = 3
2013-05-12 21:37:23.667 PerformSelectorInBackgroundTest[417:4003] Third Counter = 4
2013-05-12 21:37:23.667 PerformSelectorInBackgroundTest[417:3903] First Counter = 5
2013-05-12 21:37:23.667 PerformSelectorInBackgroundTest[417:3b03] Second Counter = 6
2013-05-12 21:37:23.668 PerformSelectorInBackgroundTest[417:4003] Third Counter = 5
2013-05-12 21:37:23.669 PerformSelectorInBackgroundTest[417:3903] First Counter = 6
2013-05-12 21:37:23.670 PerformSelectorInBackgroundTest[417:3b03] Second Counter = 7
2013-05-12 21:37:23.670 PerformSelectorInBackgroundTest[417:4003] Third Counter = 6
2013-05-12 21:37:23.671 PerformSelectorInBackgroundTest[417:3b03] Second Counter = 8
2013-05-12 21:37:23.673 PerformSelectorInBackgroundTest[417:3b03] Second Counter = 9
2013-05-12 21:37:23.673 PerformSelectorInBackgroundTest[417:4003] Third Counter = 7
2013-05-12 21:37:23.671 PerformSelectorInBackgroundTest[417:3903] First Counter = 7
2013-05-12 21:37:23.674 PerformSelectorInBackgroundTest[417:4003] Third Counter = 8
2013-05-12 21:37:23.674 PerformSelectorInBackgroundTest[417:3903] First Counter = 8
2013-05-12 21:37:23.675 PerformSelectorInBackgroundTest[417:4003] Third Counter = 9
2013-05-12 21:37:23.675 PerformSelectorInBackgroundTest[417:3903] First Counter = 9