多线程下的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的使用的更多相关文章

  1. 多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用

    本篇文章主要介绍下多线程下NSOperation.NSBlockOperation.NSInvocationOperation.NSOperationQueue的使用,列举几个简单的例子. 默认情况下 ...

  2. 【iOS开发】多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用

    http://blog.csdn.net/crycheng/article/details/21799611 本篇文章主要介绍下多线程下NSOperation.NSBlockOperation.NSI ...

  3. 多线程下的NSOperation和NSOperationQueue的使用

    多线程下的NSOperation和NSOperationQueue的使用 NSOperation和NSOperationQueue的介绍: NSOperation是Cocoa中的一个抽象类,用来封装单 ...

  4. python 类变量 在多线程下的共享与释放问题

    最近被多线程给坑了下,没意识到类变量在多线程下是共享的,还有一个就是没意识到 内存释放问题,导致越累越大 1.python 类变量 在多线程情况 下的 是共享的 2.python 类变量 在多线程情况 ...

  5. Java多线程21:多线程下的其他组件之CyclicBarrier、Callable、Future和FutureTask

    CyclicBarrier 接着讲多线程下的其他组件,第一个要讲的就是CyclicBarrier.CyclicBarrier从字面理解是指循环屏障,它可以协同多个线程,让多个线程在这个屏障前等待,直到 ...

  6. Java多线程20:多线程下的其他组件之CountDownLatch、Semaphore、Exchanger

    前言 在多线程环境下,JDK给开发者提供了许多的组件供用户使用(主要在java.util.concurrent下),使得用户不需要再去关心在具体场景下要如何写出同时兼顾线程安全性与高效率的代码.之前讲 ...

  7. 多线程下C#如何保证线程安全?

    多线程编程相对于单线程会出现一个特有的问题,就是线程安全的问题.所谓的线程安全,就是如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会同时运行这段代码.如果每次运行结果和单线程运行的结果是 ...

  8. 多线程下HashMap的死循环问题

    多线程下[HashMap]的问题: 1.多线程put操作后,get操作导致死循环.2.多线程put非NULL元素后,get操作得到NULL值.3.多线程put操作,导致元素丢失. 本次主要关注[Has ...

  9. ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 349人阅读 评论(0) 收藏

    问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...

随机推荐

  1. 字体圆润属性的使用-webkit-font-smoothing: antialiased

    字体渲染和抗锯齿技术 据称该属性在window下不起作用,不知win10如何,但是在OS和ios中会有不同的展示效果,主要也是展示在webkit内核中,以及android和ios中 大概是说字体渲染的 ...

  2. HTML5拖放API

    拖放事件事件提供了拖放可以控制几乎所有方面的拖放操作.棘手的部分是确定每个事件触发:在拖项目火:别人火下降的目标.拖动项时,以下事件(按照这个顺序): 拖曳开始拖dragend此刻你把鼠标按钮和开始移 ...

  3. 谈谈android 布局 的优化

    来自:http://www.cnblogs.com/youxilua/archive/2012/05/08/2489414.html 导言 设配android的屏幕一定是一个噩梦,就好比那些搞网页设计 ...

  4. IOS 命令行安装备忘

    1. 首先要越狱 2. 新增BIGBOSS或者苹果核的源 3. 安装MobileTerminal和MobileTerm Backgrounder (用于后台运行命令),最好r520以上版本,R530还 ...

  5. UIview lianxi

    // 创建一个和屏幕大小相同的window,记住[UIScreen mainScreen].bounds 是获取当前屏幕大小 self.window = [[[UIWindow alloc] init ...

  6. Java JPA 查询实体部分字段

    前言 相信大家在用Java JPA作为ORM的时候都会有这种困惑,就是某个表T我仅仅希望取到其中的A.B.C三个字段,可是jpa是通过Entity Class映射的方式组合查询结果的. 那么如何通过使 ...

  7. 工作总结:MFC使用Windows自带打开对话框

    代码: ; //Win98 as default LPARAM lModuleParam = ; TV_INSERTSTRUCT tvItem; CString sPathNameSel; ];//1 ...

  8. WEB工程数据库相关安装脚本写作

    1. 数据库oracle安装 2. 数据库用户创建,表空间创建,表创建 #!/bin/bash current_path=`pwd` create_tablespace=${current_path} ...

  9. Selenium 自动化验收测试

    Web 应用程序的验收测试常常涉及一些手工任务,例如打开一个浏览器,并执行一个测试用例中所描述的操作.但是手工执行的任务容易出现操作人员人为的错误,也比较费时间.因此,尽可能将这些任务自动化,以消除人 ...

  10. Rundeck,RUN起来!!

    零晨一点, 还好,跑起来了.. 满满的英文文档,肿么办?? 拿下!