zoukankan      html  css  js  c++  java
  • IOS之NSThread

    初始化:

    1.动态方法

    - (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;
    
    // 初始化线程
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
    // 设置线程的优先级(0.0 - 1.0,1.0最高级)
    thread.threadPriority = 1;
    // 开启线程
    [thread start];
    

     2.静态方法

    + (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument;
    
    [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
    // 调用完毕后,会马上创建并开启新线程
    

     3.隐式创建线程

    [self performSelectorInBackground:@selector(run) withObject:nil];
    

     获取当前线程

    NSThread *current = [NSThread currentThread];
    

     获取主线程

    NSThread *main = [NSThread mainThread];
    

     暂停当前线程

    // 暂停2s
    [NSThread sleepForTimeInterval:2];
    
    // 或者
    NSDate *date = [NSDate dateWithTimeInterval:2 sinceDate:[NSDate date]];
    [NSThread sleepUntilDate:date];
    

     线程间的通信

    1.在指定线程上执行操作

    [self performSelector:@selector(run) onThread:thread withObject:nil waitUntilDone:YES];
    

     2.在主线程上执行操作

    [self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];
    

     3.在当前线程执行操作

    [self performSelector:@selector(run) withObject:nil];
    
  • 相关阅读:
    python+selenium(环境的安装)
    Eclipse安装和配置
    Java JDK装配置
    Eclipse工具使用技巧总结
    POJ3461 Oulipo
    洛谷P3370 【模板】字符串哈希
    CH1401 兔子与兔子
    洛谷P2347 砝码称重
    洛谷P1038 神经网络
    洛谷P1807 最长路_NOI导刊2010提高(07)
  • 原文地址:https://www.cnblogs.com/sfce/p/4318927.html
Copyright © 2011-2022 走看看