Custom RunLoopMode

NSRunLoop 就是一直在循环检测,从线程start到线程end,检测inputsource(如点击,双击等操作)同步事件,检测timesource同步事件,检测到输入源会执行处理函数,首先会产生通知,corefunction向线程添加runloop observers来监听事件,意在监听事件发生时来做处理。

 perform selector在thread中被序列化执行,这样就缓和了许多在同一个thread中运行多个方法所产生的同步问题。

@interface ViewController ()
{
    NSThread *A;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton*button =[[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    button.backgroundColor= [UIColor blackColor];
    A = [[NSThread alloc] initWithTarget:self selector:@selector(AThread) object:nil]; //生成线程A
    [A start];
}
-(void)buttonAction
{
    if (A) {
      [self performSelector:@selector(BThread) onThread:A withObject:nil waitUntilDone:YES modes:@[@"CustomRunLoopMode"]];
    }
}
-(void)BThread
{
    //<NSThread: 0x7a17ae40>{number = 2, name = (null)}
}

-(void)AThread
{
    //<NSThread: 0x7a17ae40>{number = 2, name = (null)}
    BOOL isRunning = NO;
    do {
        isRunning = [[NSRunLoop currentRunLoop] runMode:@"CustomRunLoopMode" beforeDate:[NSDate distantFuture]];
    } while (!isRunning);
    /*
     BThread end
     continue code ...
    */
    //防止线程A被释放后调用
    A = nil;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值