- (void)defaulTimer1{
NSTimer *_time1=[NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
NSLog(@"%@",[NSRunLoop currentRunLoop].currentMode);
}];
[[NSRunLoop currentRunLoop] addTimer:_time1 forMode:NSDefaultRunLoopMode];
}
- (void)defaulTimer2{//scrollview滑動定時器也運行
NSTimer *_time1=[NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
NSLog(@"%@",[NSRunLoop currentRunLoop].currentMode);
}];
[[NSRunLoop currentRunLoop] addTimer:_time1 forMode:NSRunLoopCommonModes];
}
- (void)defaulTimer3{//schedule開頭的都默認加到defaultMode中
NSTimer * _timer=[NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
NSLog(@"%@",[NSRunLoop currentRunLoop].currentMode);
}];
[_timer fire];
}
//為了不阻礙主線程,再開一個子線程創(chuàng)建定時器
//創(chuàng)建子線程的RunLoop直接調用[NSRunLoop currentRunLoop];
//一定要讓子線程的runLoop跑起來, 不然的話, 子線程一結束, 運行循環(huán)立馬銷毀
- (void)defaulTimer4{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSTimer *_time1=[NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
NSLog(@"%@",[NSRunLoop currentRunLoop].currentMode);
}];
//NSRunLoop * mainRunLoop=[NSRunLoop mainRunLoop];
NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
[currentRunLoop addTimer:_time1 forMode:NSRunLoopCommonModes];
[currentRunLoop run];
});
}
//并發(fā)調多個接口,都返回更新UI
dispatch_group_enter(_group);
[_userOtherViewModel getPublish:_userID minID:@"" callback:^(NSInteger state, NSString *msg) {
dispatch_group_leave(_group);
}];
dispatch_group_enter(_group);
[_userOtherViewModel getMyComment:_userID minID:@"" callback:^(NSInteger state, NSString *msg) {
dispatch_group_leave(_group);
}];
dispatch_group_notify(_group, dispatch_get_main_queue(), ^{
[ self updateUI];
});
- (void)semaphore{
//對有限資源的約束
dispatch_semaphore_t semaphore=dispatch_semaphore_create(1);
//value約束幾個線程訪問資源 getdtablesize()取得程序所有線程的個數
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);//如果有人占用資源微猖,我一直等待
NSLog(@"我有了資源");
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);//如果有人占用資源绣夺,我一直等待
NSLog(@"我有了資源1");
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
sleep(2);
NSLog(@"在做事");//比如對文件操作或播視頻
//訪問資源的這個人走時發(fā)送一個信息量
dispatch_semaphore_signal(semaphore);
});
}