Create a resident thread on iOS

Source: Internet
Author: User

When a thread is created and it is expected to exist, but often the thread we create is stopped after execution and cannot be reused again, then how to create a thread that will allow him to work again, this time it will need to use the runloop. The following is an example of what I wrote:
#import "LongThreadDemoController.h" @interface Longthreaddemocontroller () @property (nonatomic, strong) Nsthread * thread; @end @implementation longthreaddemocontroller-(void) viewdidload {[Super viewdidload]; Self.view.backgroundColor = [Uicolor Whitecolor]; Self.title = @ "resident thread Demo"; }-(void) Threadrunlooppoint: (ID) __unused object{NSLog (@ "%@", Nsstringfromselector (_cmd)); @autoreleasepool {[[Nsthread CurrentThread] setname:@ "Changzhuthread"]; Nsrunloop *runloop = [Nsrunloop currentrunloop]; Here the main is to listen to a port, the purpose is to make this Thread does not reclaim [Runloop Addport:[nsmachport Port] Formode:nsdefaultrunloopmode]; [Runloop Run]; }}-(Nsthread *) thread{if (!_thread) {_thread = [[Nsthread alloc] initwithtarget:self selector: @selector (THR Eadrunlooppoint:) Object:nil]; [_thread start]; } return _thread; }-(void) test{NSLog (@ "%s", __func__);} -(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{[sElf Performselector: @selector (test) onThread:self.thread Withobject:nil waituntildone:no modes:@[ Nsdefaultrunloopmode]]; }

  

Create a resident thread on iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.