zoukankan      html  css  js  c++  java
  • 线程希望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,一边拜乔帮主的果粉自以为是果粉,其实在乔帮主的眼里是不折不扣的叛徒。

  • 相关阅读:
    Database "C:/Users/com/test" not found, either precreate it or allow remote database creation (not recommended in secure environments) [90149200] 90149/90149 (帮助) 解决
    python学习——windows下pandarallel cannot find context for ‘fork‘
    mysql as if 多个,mysql as多个if语句
    新安装 eclips 下载进度条不动,解决办法
    Java EE: XML Schemas for Java EE Deployment Descriptors
    java 删除所有早于N天的文件(递归选项,它遍历子文件夹)
    pandas groupby 取每组的前几行记录方法
    python dataframe rename_详解pandas DataFrame修改行列名
    Spring MVC 整合Velocity及用法说明
    java判断日期大小,大于等于,小于等于
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3074675.html
Copyright © 2011-2022 走看看