欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-06 19:29:16
1 前言 使用 nsobject 的实例方法 performselectorinbackground:withobject: ,来创建一个线程,而不需要直接处理线程。 2...

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