一:多线程
1,NSThread创建线程
a,NSThread的类方法创建线程
[NSThread detachNewThreadSelector:@selector(doing) toTarget:self withObject:nil];
withObject 参数 下面几个方法类似
b,构造方法创建线程需要start
NSThread *th=[[NSThread alloc]initWithTarget:self selector:@selector(doing) object:nil];
[th start];
c,View创建
[self performSelectorInBackground:@selector(doing) withObject:nil];
2,Operation创建线程
a,Operation创建线程
//创建 Operation队列,add创建 NSOperationQueue *queue =[[NSOperationQueue alloc]init]; [queue addOperationWithBlock:^{ //执行方法 }];
b,Operation启动多个线程,可设置线程的优先级
NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downloadImage:) object:@"1"]; NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downloadImage:) object:@"2"]; [operationQueue addOperation:operation1]; [operationQueue addOperation:operation2];
3,GCD创建线程
dispatch_queue_t queue=dispatch_queue_create("baihe", nil); dispatch_async(queue, ^{ });
二:定时器
看下面的定时器操作
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doing) userInfo:nil repeats:YES];该语句可能不会输出 ,可能会被return
解决办法://获得当前线程,防止被return,无法执行
[[NSRunLoopcurrentRunLoop] run];
一般不在主线程中执行定时操作,开启线程使用自动释放池操作
/** TimerInterval : 执行之前等待的时间。比如设置成1.0,就代表1秒后执行方法 target : 需要执行方法的对象。 selector : 需要执行的方法 repeats : 是否需要循环 */ @autoreleasepool { NSTimer *timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doing) userInfo:nil repeats:YES]; //RUNLoop管理定时器 // [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode]; //获得当前线程,防止被return,无法执行 [[NSRunLoop currentRunLoop] run]; // [timer invalidate];//停止定时器
相关推荐
java多线程和定时器学习,代码和文档都有,很详细
通过简单例子实现多线程,把界面和数据计算分开,又在其中运用定时器
MFC中利用多线程实现定时器,对于学习多线程和 ontimer的同学有用
C#多线程实现定时器
问题:在线程中,我想使用个定时器,然后通过点击主线程的按钮来通知此线程的定时器开启和停止;发现会出现"无法关闭在另一个线程的定时器"的提示 解决办法:无法关闭在另一个线程的定时器,是因为定时器要在新的...
delphI自带的定时控件是以消息方式产生的,当程序忙时就会收不到定时消息。这个线程定时器可解决这个问题。
封装的定时器类,采用线程实现,实现定时、相对定时、周期定时执行的功能
linux线程实现定时器源码
个人简单设计的定时器与线程的使用,简单明了,适用于C++初学者
CVI学习文件-多线程 异步定时器(修改增加学习版)
实现C#2008里面的定时器功能的打开和关闭,线程的打开和关闭! 已经做成简单界面便于深入开发
timerfd的定时器和epoll监听 比较通用 里面有makefile 直接编译即可
NULL 博文链接:https://heisetoufa.iteye.com/blog/1684467
Linux中通过多线程多定时器来完成不同的任务;Linux中通过多线程多定时器来完成不同的任务;
LinuxWindows平台C++多线程及定时器毕业论文.docx
QT中在子线程中调用定时器 https://blog.csdn.net/wb175208/article/details/108476275
Javascript定时器 一 单线程
c#定时器+多线程,定时更新数据 定时器方法,可发送定时邮件
// ii)用户需要set_a_timer注册自定义定时器时,在timer_manage管理结构中记录这个定时器的回调函数和定时周期等参数; // // iii)当基本的时间单位到期后(如SIGALRM信号到达时),遍历整个timer_manage,如果有...
用模式对话框来展示操作进度。 具体操作在线程中进行。 通过在线程中进行操作并修改表示进度的变量, 然后再对话框中通过定时器来设置进度,实现多线程情况下的进度控制。