• perform系列  通知  timer等研究


    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        //[self performSelectorInBackground:@selector(tt) withObject:nil];
    //    NSLog(@"%@",[NSThread currentThread]);
    //    NSLog(@"%d",[NSThread isMultiThreaded]);
    //    NSLog(@"%@",[NSThread mainThread]);
        
    //    [NSThread detachNewThreadSelector:@selector(tt:) toTarget:self withObject:@"yyyyy"];
    //    sleep(3);
    //    NSLog(@"----------");
        
        //[self performSelectorInBackground:@selector(read) withObject:nil];
        //[self performSelector:@selector(write:) withObject:@"aaaa" afterDelay:3.0];
        //NSDistributedNotificationCenter
        //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(write:) name:@"ccc" object:nil];
        //_ob = [[myobject alloc] init];
        //_tt = [[[[thb alloc] init] autorelease] autorelease];
        
       
       
        //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(write1:) name:@"ccc" object:nil];
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(write2:) name:@"ccc" object:nil];
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(write3:) name:@"ccc" object:nil];
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(write4:) name:@"ccc" object:nil];
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(write5:) name:@"ccc" object:nil]
    ;
        
    //    _queue = [[NSOperationQueue alloc] init];
    //    [[NSNotificationCenter defaultCenter] addObserverForName:@"ccc" object:nil queue:_queue usingBlock:^(NSNotification *note) {
    //        sleep(5);
    //        NSLog(@"%@",note);
    //    }];
        //NSLog(@"[NSRunLoop currentRunLoop] = %@",[NSRunLoop currentRunLoop]);
        
        //[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(threadGo) userInfo:nil repeats:NO];
        
        //[[NSMyNotificationCenter defaultCenter] addObserver:self forKeyPath:@"parame" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
        //[self performSelector:@selector(write1:) withObject:nil];
        //[_ob addObserver:self forKeyPath:@"pix" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
        //NSLog(@"[_tt retainCount] = %d",[_tt retainCount]);
    //    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //        
    //    });
        //[NSThread detachNewThreadSelector:@selector(threadGo) toTarget:self withObject:nil];
        //_thread = [[NSThread alloc] init];
        //[_thread start];
    //    while (!_finished) {
    //        NSLog(@"runloop…");
    //        
    //        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    //        NSLog(@"runloop end.");
    //    }
       //NSLog(@"%@",[NSRunLoop mainRunLoop]);
        //[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(write:) userInfo:nil repeats:YES];
        //[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(write1:) userInfo:nil repeats:YES];
        
       

       

        
        
        //[self performSelector:@selector(write:) onThread:_thread withObject:nil waitUntilDone:YES];
        NSLog(@"77777777");
        //NSLog(@"NSNotificationQueue defaultQueue] = %@",[NSNotificationQueue defaultQueue]);
    }

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
        if ([keyPath isEqualToString:@"parame"]) {
            //sleep(3);
            NSLog(@"parame");
        }
        
        if ([keyPath isEqualToString:@"pix"]) {
            NSLog(@"pix");
        }
    }

    - (void)write:(id)par{
        //NSLog(@"par = %@",par);
        NSLog(@"go in");
         //NSLog(@"%@",[NSRunLoop currentRunLoop]);
        sleep(2);
        //label.text = [NSString stringWithFormat:@"%@",[NSDate date]];
        NSLog(@"go out");
        //SRunLoop *runloop = [NSRunLoop currentRunLoop];
        //NSTimer 对象不管调用哪个方法都不会重新创建一个新的线程,只会作为一个timer源添加到当前所在的runLoop,并让runloop执行它。并且timer的响应是同步的,如果其响应函数太耗时会阻塞runloop中后面事件的响应,比如在主线程中有这样的情况,那么在timer响应函数期间,会卡死界面,并阻止后面的事件响应 要排队
        //NSTimer *timer = [NSTimer timerWithTimeInterval:0 target:self selector:@selector(threadGo) userInfo:nil repeats:NO];
        //[runloop addTimer:timer forMode:NSDefaultRunLoopMode];
        
        //[runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        
        //NSLog(@"runloop = %@",runloop);
        //NSLog(@"%@",[NSString stringWithFormat:@"%@--%d",par.object,0]);
    }
    - (void)threadGo {
        @autoreleasepool {
           // NSLog(@"NSNotificationQueue defaultQueue]111 = %@",[NSNotificationQueue defaultQueue]);
             //NSNotification *no = [NSNotification notificationWithName:@"ccc" object:nil userInfo:nil];
            //[[NSNotificationQueue defaultQueue] enqueueNotification:no postingStyle:NSPostASAP];
            //[[NSNotificationCenter defaultCenter] postNotificationName:@"ccc" object:nil];
            
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(write1:) name:@"ccc" object:nil];
            NSLog(@"==============");
    //        NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(write:) userInfo:nil repeats:YES];
    //       
    //        //[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(write:) userInfo:nil repeats:YES];
    //        //while (!_finished) {
    //        //NSLog(@"runloop…");
    //        //_loop = [[NSRunLoop currentRunLoop] getCFRunLoop];
    //        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    //        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
            NSLog(@"runloop end.");
            //}
        }
        
        //NSTimer *timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(write1:) userInfo:nil repeats:YES];
        //CFRunLoopRef loop = CFRunLoopGetCurrent();
    //    CFRunLoopAddTimer(CFRunLoopGetCurrent(), CFRunLoopTimerCreate(
  • 相关阅读:
    (转载)你好,C++(25)函数调用和它背后的故事5.1.2 函数调用机制
    (转载)你好,C++(24)好大一个箱子!5.1.1 函数的声明和定义
    (转载)你好,C++(23) 4.4.2 工资程序成长记:用数组处理批量数据,用循环结构执行重复动作
    (转载)你好,C++(22) 排排坐,吃果果——4.3.3 for循环:某个范围内…每个都…
    【NOI2002T4】荒岛野人-扩展欧几里得
    【POJ1743】Musical Theme-后缀数组+二分答案
    【POJ1743】Musical Theme-后缀数组+二分答案
    【NOI2014T1】起床困难综合症-贪心+位运算
    【NOI2014T1】起床困难综合症-贪心+位运算
    【APIO2012T1】派遣-贪心+左偏树
  • 原文地址:https://www.cnblogs.com/cnsec/p/11515804.html
Copyright © 2020-2023  润新知