当前位置: 移动技术网 > 移动技术>移动开发>IOS > IOS开发(67)之简单的线程方法

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

2019年05月12日  | 移动技术网移动技术  | 我要评论

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

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网