iOSIN-线程

NSThread

    //方式一:初始化方式,需要手动启动
    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething:) object:@"threadName1"];
    [thread1 start];//手动启动
    
    //方式二:构造器方式,自动启动
    [NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:@"threadName2"];
    
    //方式三:performSelector...方法创建
    [self performSelectorInBackground:@selector(doSomething:) withObject:@"threadName3"];
    
    //方式四:主线程
    [self performSelectorOnMainThread:@selector(doSomething:) withObject:@"threadNameMain" waitUntilDone:YES];

NSOperation

//基本使用
- (void)cjl_testBaseNSOperation{
    //处理事务
    NSInvocationOperation *op =  [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(handleInvocation::) object:@"CJL"];
    //创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    //操作加入队列
    [queue addOperation:op];
    
}
- (void)handleInvocation:(id)operation{
    NSLog(@"%@ - %@", operation, [NSThread currentThread]);
}

你可能感兴趣的:(iOSIN-线程)