zoukankan      html  css  js  c++  java
  • 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(
  • 相关阅读:
    sqlserver-一次updlock和withnolock和with check option 的报错原因分析
    类库文件引用web服务报错解决方法-在 ServiceModel 客户端配置部分中,找不到引用协定的默认终结点元素
    用timer自定义计划任务时间
    console 程序随系统启动及隐藏当前程序窗口
    爱积多合作农场正式上线试运营
    CSS-三列布局
    CSS-垂直居中
    form表单重置
    CSS3 选择器
    Tab选项卡
  • 原文地址:https://www.cnblogs.com/cnsec/p/11515804.html
Copyright © 2011-2022 走看看