iOS 循环线程

来源:互联网 发布:python程序right方法 编辑:程序博客网 时间:2024/06/06 12:37

#import "ViewController.h"


@interface ViewController ()


@property (nonatomic,assign) CFRunLoopRef currentLoop;


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    NSThread *thread = [[NSThreadalloc] initWithTarget:selfselector:@selector(startThread:)object:nil];

    [thread start];

}


- (void)startThread:(id)sender

{

    _currentLoopCFRunLoopGetCurrent();//子线程的runloop引用

    

    @autoreleasepool {

        [selfhandle];

        NSLog(@"1");

        CFRunLoopRun();

        NSLog(@"234");

    }

}


-(void)handle

{

    [NSTimerscheduledTimerWithTimeInterval:0.5target:selfselector:@selector(realHandle)userInfo:nilrepeats:YES];

}


-(void)realHandle

{

    sleep(3);

    

    CFRunLoopStop(_currentLoop);


}


@end

0 0
原创粉丝点击