IOS開發(67)之簡單的線程方法

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.