zoukankan      html  css  js  c++  java
  • 子线程定时器的创建

    • 在子线程创建定时器时,需要手动开启子线程的运行循环。
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    
    	// 在子线程创建定时器
    	/*
    	scheduled 或 timer 方式创建
    	*/
    	self.timer = [NSTimer timerWithTimeInterval:1.0 
    	target:self 
    	selector:@selector(updateTimer:) 
    	userInfo:nil 
    	repeats:YES];            
    	[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    
    	// 启动子线程的运行循环
    	/*
    	这句代码就是一个死循环!如果不停止运行循环,不会执行添加到此句之后的任何代码
    	*/
    	CFRunLoopRun();
    
    	// 停止子线程运行循环之前,不会执行添加到此处的任何代码
    });
    // 定时器执行操作方法
    
    - (void)updateTimer {
    
    	static int num = 0;
    
    	num++;
    
    	// 满足条件后,停止当前的运行循环
    	if (num == 8) {
    
    		// 停止当前的运行循环
    		/*
    		一旦停止了运行循环,后续代码能够执行,执行完毕后,线程被自动销毁
    		*/
    		CFRunLoopStop(CFRunLoopGetCurrent());
    	}
    }
    
  • 相关阅读:
    pexpect模块
    Python正则表达式
    telnetlib
    paramiko
    threadpool和Queue
    logging
    Python异常
    Python迭代器
    程序员工资那么高,却从不炫富?网友回复让人“笑喷了”!
    小白到web前端工程师需要学习哪些知识?
  • 原文地址:https://www.cnblogs.com/CH520/p/9451938.html
Copyright © 2011-2022 走看看