当前位置:编程学习 > wap >>

IOS开发(67)之简单的线程方法

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

 

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,