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

时间紧张,先记一笔,后续优化与完善。

    

1 前言

    

使用 NSObject 的实例方法 performSelectorInBackground:withObject: ,来创立一个线程,而不需要直接处置线程。 

    

2 代码实例

    ZYAppDelegate.m

    

- (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

    

3 结语

    以上是全部内容,希望对大家有所帮助。

文章结束给大家分享下程序员的一些笑话语录:  一边用着越狱的ip,一边拜乔帮主的果粉自以为是果粉,其实在乔帮主的眼里是不折不扣的叛徒。

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3074675.html