多线程下的performSelector和NSThread的使用
多线程下的performSelector和NSThread的使用
NSThread的多线程使用:
我们可以使用这两种方法来使用线程中的问题
- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument
+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument
第一个是实例方法,传统方法创建一个线程,这个方法必须要在调用了[thread start]的时候才可以运行。
第二个是类方法,创建了之后,会立即调用的。
1、[NSThread detachNewThreadSelector:@selector(selector) toTarget:self withObject:nil];
2、NSThread* myThread = [[NSThread alloc] initWithTarget:self
selector:@selector(selector)
object:nil];
[myThread start];
performSelector来调用多线程:
[self
performSelector:@selector(selector)
onThread:[NSThread
mainThread] withObject:nil
waitUntilDone:YES];
[self
performSelectorInBackground:@selector(selector)
withObject:nil];
第一个为可以指定调用线程的方法。
第二个为可以指定在后台调用事件,线程系统分配的方法。
[self performSelector:@selector(performSelectorMethods) onThread:[NSThread mainThread] withObject:nil waitUntilDone:YES];
[self performSelectorInBackground:@selector(performselectorINbackgroud) withObject:nil];
下面是使用以上方法的范例:
@implementation NSThreadUSE
- (void)viewDidLoad {
[super viewDidLoad];
// 使用传统方法创建一个线程。这个方法必须要在调用了[thread start]的时候才可以运行
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(initWithTargetMethods) object:nil];
[thread start];
// 使用类方法来调用线程,这个方式会立即调用并执行线程
[NSThread detachNewThreadSelector:@selector(detachNewThreadMethods) toTarget:self withObject:nil];
// 使用performSelector的方法来调用函数,并且设置调用方法的线程
[self performSelector:@selector(performSelectorMethods) onThread:[NSThread mainThread] withObject:nil waitUntilDone:YES];
// 使用performSelectorInBackground这个方法来被调用函数,这个函数是在后台调用的
[self performSelectorInBackground:@selector(performselectorINbackgroud) withObject:nil];
}
#pragma mark NSThread Methods
-(void) initWithTargetMethods{
NSLog(@"initWithTargetMethods is ---------------%@", [NSThread currentThread]);
}
-(void) detachNewThreadMethods{
NSLog(@"detachNewThreadMethods is ---------------%@", [NSThread currentThread]);
}
-(void) performSelectorMethods{
NSLog(@"performSelectorMethods is ---------------%@", [NSThread currentThread]);
}
-(void) performselectorINbackgroud{
NSLog(@"performselectorINbackgroud is ---------------%@", [NSThread currentThread]);
}
@end
多线程下的performSelector和NSThread的使用的更多相关文章
- 多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用
本篇文章主要介绍下多线程下NSOperation.NSBlockOperation.NSInvocationOperation.NSOperationQueue的使用,列举几个简单的例子. 默认情况下 ...
- 【iOS开发】多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用
http://blog.csdn.net/crycheng/article/details/21799611 本篇文章主要介绍下多线程下NSOperation.NSBlockOperation.NSI ...
- 多线程下的NSOperation和NSOperationQueue的使用
多线程下的NSOperation和NSOperationQueue的使用 NSOperation和NSOperationQueue的介绍: NSOperation是Cocoa中的一个抽象类,用来封装单 ...
- python 类变量 在多线程下的共享与释放问题
最近被多线程给坑了下,没意识到类变量在多线程下是共享的,还有一个就是没意识到 内存释放问题,导致越累越大 1.python 类变量 在多线程情况 下的 是共享的 2.python 类变量 在多线程情况 ...
- Java多线程21:多线程下的其他组件之CyclicBarrier、Callable、Future和FutureTask
CyclicBarrier 接着讲多线程下的其他组件,第一个要讲的就是CyclicBarrier.CyclicBarrier从字面理解是指循环屏障,它可以协同多个线程,让多个线程在这个屏障前等待,直到 ...
- Java多线程20:多线程下的其他组件之CountDownLatch、Semaphore、Exchanger
前言 在多线程环境下,JDK给开发者提供了许多的组件供用户使用(主要在java.util.concurrent下),使得用户不需要再去关心在具体场景下要如何写出同时兼顾线程安全性与高效率的代码.之前讲 ...
- 多线程下C#如何保证线程安全?
多线程编程相对于单线程会出现一个特有的问题,就是线程安全的问题.所谓的线程安全,就是如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会同时运行这段代码.如果每次运行结果和单线程运行的结果是 ...
- 多线程下HashMap的死循环问题
多线程下[HashMap]的问题: 1.多线程put操作后,get操作导致死循环.2.多线程put非NULL元素后,get操作得到NULL值.3.多线程put操作,导致元素丢失. 本次主要关注[Has ...
- ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 349人阅读 评论(0) 收藏
问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...
随机推荐
- H5相关
对于容器元素,尤其在做移动端产品时候,我们很自然会让其居中定位: .container { position: absolute; left: %; top: %; transform: transl ...
- js获取返回首页
<script>setTimeout(function(){ window.location.href="http://"+window.location.hos ...
- [cocoapods速成] cocoapods的基本用法和自制 podspec
1 安装方法 主要命令: sudo gem install cocoapods ------------------------------------------------------------ ...
- SELECT ... LOCK IN SHARE MODE和SELECT ... FOR UPDATE locks在RR模式下可以看到最新的记录
14.5.2.4 Locking Reads 锁定读: 如果你查询数据然后插入或者修改相关数据在相同的事务里, 常规的SELECT 语句不能给予足够的保护. 其他事务可以修改或者删除你刚查询相同的记录 ...
- texedo 分布式事务
1.问题现象 但是实际情况,完全出乎笔者的想法.检查一般对象数据表锁定,只需要检查v$locked_object和v$transaction视图,就可以定位到具体人.但是检查之后的结果如下: SQL& ...
- 解决VS2010中产生的ipch文件夹和sdf文件
纯粹参考他人博文,转载时注明参考链接!!! 参考链接: http://blog.sina.com.cn/s/blog_707a4cbf0100r5jq.html 在VS2010开发的项目中,ipch文 ...
- bzoj2588
一开始一看树上的操作,就无脑写了树链剖分+主席树 然后果断T了,因为树链剖分+主席树必然带来两个log的复杂度 而且树链剖分复杂度还比较大…… 后来发现其实没必要,在这道题,我们可以直接利用主席树维护 ...
- Java编译原理
http://wenku.baidu.com/view/f9b1734b87c24028915fc3a3.html Java编译原理 1. 关于动态加载机制 学习Java比C++更容易理解OOP的思想 ...
- HDU 5501 背包问题
需要按照B/C的值从大到小排序. #include<cstdio> #include<cstring> #include<iostream> #include< ...
- HDU 1495 非常可乐 BFS 搜索
http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目就不说了, 说说思路! 倒可乐 无非有6种情况: 1. S 向 M 倒 2. S 向 N 倒 3. N ...