多线程下的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. php中判断变量是否为空

    从数据库中取出值后判断是否为空,这个看起来很简单,只要和null比较一下就可以了,其实不然, if($obj==null){ } 这样写会报错的:Notice: Trying to get prope ...

  2. Aspnet MVC 异步调用

    一个简图来描述下Aspnet MVC下的异步调用 { request } / \/ -------ISS------- > work thread | \ | \ route - aysn co ...

  3. kafka笔记-Kafka在zookeeper中的存储结构【转】

    参考链接:apache kafka系列之在zookeeper中存储结构  http://blog.csdn.net/lizhitao/article/details/23744675 1.topic注 ...

  4. loadView与viewDidLoad不同 && loadView学习总结

    loadView学习总结 UIViewController类或其子类会在初始化时创建一个UIView对象,会作为控制器的默认视图显示出来,可以通过self.view寻址访问.但没有调用loadView ...

  5. IOS--UITextFiled的使用方法

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for ...

  6. STM32F10x 学习笔记5(USART实现串口通讯 1)

    STM32F10x 系列单片机中都包含了USART 模块,所谓USART,就是通用同步异步收发器.通用同步异步收发器(USART)提供了一种灵活的方法与使用工业标准NRZ异步串行数据格式的外部设备之间 ...

  7. Tomcat error: A child container failed during start

    Tomcat error: A child container failed during start java.lang.NoClassDefFoundError: org/quartz/Sched ...

  8. PL/SQL developer export/import (转)

    export/import图标为灰色:原因:相关应用程序没有关联菜单栏 --> Tools --> Import Tables... --> Oracle Import Export ...

  9. 【HDOJ】5128

    暴力+计算几何. /* 5128 */ #include <iostream> #include <algorithm> #include <cstdio> #in ...

  10. 实战weblogic集群之应用部署

    一.创建应用发布目录,上传应用包. 1.在10.70.52.11-14的/app/sinova目录下建立applications目录(名称可以自定义),作为我们应用的发布目录. $ mkdir /ap ...