IOS RunLoop 常駐線程的實現

來源:互聯網
上載者:User

標籤:orm   reserve   存在   com   while   nonatomic   tom   比較   for   

線程常駐,正如其名,我們要實現的事讓一個線程長期存在,不被銷毀。

這時會有人說,那還不簡單嗎。

但是這裡我們要實現的事如何讓線程座椅待命,而且並不是主線程。

首先介紹一下正常情況下的線程使用。

////  ViewController.m//  CX RunLoop 常駐線程的實現////  Created by ma c on 16/3/30.//  Copyright ? 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "CXThread.h"@interface ViewController ()@property (nonatomic, strong)CXThread * thread;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];     NSThread* thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil];        [thread start];}-(void)run{        NSLog(@"run -- 旭寶愛吃魚");    }-(void)test{        NSLog(@"test -- 旭寶愛吃魚 %@",[NSThread currentThread]);    }-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{        [self test];        //讓test方法線上程thread上實現//    [self performSelector:@selector(test) onThread:_thread withObject:nil waitUntilDone:nil];    }@end

上面的代碼知識簡單的實現了線程的使用。

下面是其(注意線程的銷毀)

實際上test與thread並沒有關係。

我知識簡單的讓其輸出預設的主線程日誌,以供後面對比。

下面是讓thread為全域變數

////  ViewController.m//  CX RunLoop 常駐線程的實現////  Created by ma c on 16/3/30.//  Copyright ? 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "CXThread.h"@interface ViewController ()@property (nonatomic, strong)CXThread * thread;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];     _thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil];        [_thread start];}-(void)run{        NSLog(@"run -- 旭寶愛吃魚");    }-(void)test{        NSLog(@"test -- 旭寶愛吃魚 %@",[NSThread currentThread]);    }-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{        [self test];        //讓test方法線上程thread上實現//    [self performSelector:@selector(test) onThread:_thread withObject:nil waitUntilDone:nil];    }@end

由我們可以發現。thread並沒有銷毀。而且test,依舊是在主線程上實現的。

但我們想要的是test在thread上實現(實際開發中是不允許耗時操作在主線程中的)

我們讓test在thread中實現:(注意蝦米那方法並不成功)

////  ViewController.m//  CX RunLoop 常駐線程的實現////  Created by ma c on 16/3/30.//  Copyright ? 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "CXThread.h"@interface ViewController ()@property (nonatomic, strong)CXThread * thread;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];     _thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil];        [_thread start];}-(void)run{        NSLog(@"run -- 旭寶愛吃魚");    }-(void)test{        NSLog(@"test -- 旭寶愛吃魚 %@",[NSThread currentThread]);    }-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //    讓test方法線上程thread上實現    [self performSelector:@selector(test) onThread:_thread withObject:nil waitUntilDone:YES];    }@end

為什麼會不成功呢??(我真的點擊了)

原因是我們只是單純的建立了一個線程。。。很單純的。。。考慮一下我們該怎麼做。

那麼我們有兩種做法實現。

方法一(比較正常的方法)

////  ViewController.m//  CX RunLoop 常駐線程的實現////  Created by ma c on 16/3/30.//  Copyright ? 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "CXThread.h"@interface ViewController ()@property (nonatomic, strong)CXThread * thread;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];     _thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil];        [_thread start];}-(void)run{        NSLog(@"run -- 旭寶愛吃魚");    //添加Port 即時監聽    [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];    //添加runloop    [[NSRunLoop currentRunLoop]run];        }-(void)test{        NSLog(@"test -- 旭寶愛吃魚 %@",[NSThread currentThread]);    }-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //    讓test方法線上程thread上實現    [self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:NO];    }@end

就是這麼簡單。

方法二

////  ViewController.m//  CX RunLoop 常駐線程的實現////  Created by ma c on 16/3/30.//  Copyright ? 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "CXThread.h"@interface ViewController ()@property (nonatomic, strong)CXThread * thread;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];     _thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil];        [_thread start];}-(void)run{        NSLog(@"run -- 旭寶愛吃魚");        while (1) {        //添加runloop        [[NSRunLoop currentRunLoop]run];    }}-(void)test{        NSLog(@"test -- 旭寶愛吃魚 %@",[NSThread currentThread]);    }-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //    讓test方法線上程thread上實現    [self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:NO];    }@end

 

IOS RunLoop 常駐線程的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.