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(
posted @ 2013-12-24 16:07  SEC.VIP_网络安全服务  阅读(112)  评论(0编辑  收藏  举报