zoukankan      html  css  js  c++  java
  • 多线程之NSThread

    关于多线程会有一系列如下:
    多线程之概念解析 

    多线程之pthread, NSThread, NSOperation, GCD

    多线程之NSThread

    多线程之NSOperation

    多线程之GCD

    一,创建线程

    • 动态创建
    /**
     动态创建
     */
    - (void)dynamicCreateThread {
        // 动态创建
        // 动态创建的线程,必须调用 start ,线程才会跑起来
        NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(dynamicRun:) object:@1];
        thread.threadPriority = 1;//0-1,1的优先级最高
        thread.name = @"dynamic thread";
        [thread start];
        
        // iOS10 以后新添加的方法
        NSThread *newThread = [[NSThread alloc] initWithBlock:^{
            NSLog(@"block operation");
        }];
        [newThread start];
    }
    
    
    
    - (void)dynamicRun:(NSNumber *)index {
        NSLog(@"thread = %@", [NSThread currentThread]);
        NSLog(@"index = %@", index);
        NSLog(@"dynamic run");
        NSLog(@"name = %@", [NSThread currentThread].name);
    }
    • 静态创建

    /** 静态创建 */ - (void)staticCreateThread { // 静态创建 [NSThread detachNewThreadSelector:@selector(staticRun) toTarget:self withObject:nil]; // ios 10以后才出现 [NSThread detachNewThreadWithBlock:^{ [self staticRun]; }]; }

    • 隐式创建
    /**
     隐式创建
     */
    - (void)backgroundCreateThread {
        [self performSelectorInBackground:@selector(backgroundRun) withObject:nil];
    }
    • 线程通信
    - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
    
    - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait;

    二.属性

     @property (class, readonly, strong) NSThread *currentThread;//获取当前线程

    @property (nullable, copy) NSString *name // 线程名
    @property double threadPriority //优先级,0-1, 1的优先级最高
    @property NSUInteger stackSize // 线程所占用内存
    @property (readonly) BOOL isMainThread // 是否为主线程

    状态相关

    @property (readonly, getter=isExecuting) BOOL executing 
    @property (readonly, getter=isFinished) BOOL finished 
    @property (readonly, getter=isCancelled) BOOL cancelled


    三.API

    + (BOOL)isMultiThreaded;// 判断当前线程是否是多线程
    
    //线程操作
    
    - (void)start;
    
    - (void)cancel;
    
    + (void)exit;
    
    - (void)main;// thread body method
    
    // 线程阻塞
    
    + (void)sleepUntilDate:(NSDate *)date;
    
    + (void)sleepForTimeInterval:(NSTimeInterval)ti;
    
     
    // 获取线程
    @property (class, readonly, strong) NSThread *currentThread;//获取当前线程
    @property (class, readonly, strong) NSThread *mainThread;//获取主线程

    四.自定义NSThread

      注意:  通过静态方法创建的 thread不会执行 main 方法

      











  • 相关阅读:
    [LeetCode] 464. Can I Win 我能赢吗
    [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
    [LeetCode] 244. Shortest Word Distance II 最短单词距离 II
    [LeetCode] 245. Shortest Word Distance III 最短单词距离 III
    [LeetCode] 243. Shortest Word Distance 最短单词距离
    [LeetCode] 229. Majority Element II 多数元素 II
    [LeetCode] 291. Word Pattern II 词语模式 II
    [LeetCode] 290. Word Pattern 单词模式
    C#中对 XML节点进行添加,删除,查找和删除操作
    VS2010在C#头文件中添加文件注释的方法(转)
  • 原文地址:https://www.cnblogs.com/shidaying/p/6931333.html
Copyright © 2011-2022 走看看