• IOS NSThread


    任何一个 iOS 应用程序都是由一个或者多个线程构成的。无论你是否使用了多线程编程技术,至少有 1 个 线程被创建。多线程就是为了提高引用程序的工作效率!避免阻塞主线程!当我们没有用任何多线程技术的话,默认情况下,是在程序的主线程中执行相关操作!(因此不要在主线程中实现耗时方法)。

    比较简单,直接上代码。

     1 //
     2 //  ViewController.m
     3 //  CXNSThread
     4 //
     5 //  Created by ma c on 16/3/15.
     6 //  Copyright © 2016年 xubaoaichiyu. All rights reserved.
     7 //
     8 #import "ViewController.h"
     9 
    10 @interface ViewController ()
    11 
    12 @end
    13 
    14 @implementation ViewController
    15 
    16 - (void)viewDidLoad {
    17     [super viewDidLoad];
    18 
    19     /*
    20      
    21      常用方法
    22      获取当前线程 NSThread * thread = [NSThread currentThread];
    23      获取主线程 NSThread * main = [NSThread mainThread];
    24      暂停多少秒 [NSThread sleepForTimeInterval:2];
    25      暂停到什么时间段 [NSThread sleepUntilDate:2];
    26      强制停止线程 [NSThread exit];(一旦线程停止 就不能再次开启任务)。
    27      
    28      */
    29     /*
    30      
    31      线程间的通信
    32      self performSelector:<#(SEL)#> withObject:<#(id)#> withObject:<#(id)#>
    33      self performSelectorOnMainThread:<#(nonnull SEL)#> withObject:<#(nullable id)#> waitUntilDone:<#(BOOL)#>
    34      self performSelector:<#(nonnull SEL)#> onThread:<#(nonnull NSThread *)#> withObject:<#(nullable id)#> waitUntilDone:<#(BOOL)#>
    35      
    36      */
    37     /*
    38      
    39      优点:NSThread比其他两种对线程方案较轻量级,更直观的控制线程对象
    40      
    41      缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据加锁会有一定开销。
    42      ##(加锁)
    43      
    44      @synchronized(sel) {
    45      <#statements#>
    46      }
    47      
    48      ##
    49      
    50      */
    51 
    52         //初始化->动态方法
    53     NSThread * threadOne = [[NSThread alloc]initWithTarget:self selector:@selector(threadOneAction) object:nil];
    54     
    55     //设置线程名称
    56     threadOne.name = @"threadOne";
    57     
    58     //设置优先级(low ~ height)(0~1)
    59     threadOne.threadPriority = 1;
    60     
    61     //开启线程
    62     [threadOne start];
    63     
    64     //静态方法
    65     [NSThread detachNewThreadSelector:@selector(threadTwoAction) toTarget:self withObject:nil];
    66     
    67     //隐式创建
    68     [self performSelectorInBackground:@selector(threadThreeAction) withObject:nil];
    69     
    70     NSLog(@"Main->%@",[NSThread currentThread]);
    71     
    72 }
    73 
    74 -(void)threadOneAction{
    75     
    76     //log当前线程
    77     NSLog(@"One->%@",[NSThread currentThread]);
    78     
    79 }
    80 
    81 -(void)threadTwoAction{
    82     
    83     NSLog(@"Two->%@",[NSThread currentThread]);
    84     
    85 }
    86 
    87 -(void)threadThreeAction{
    88     
    89     NSLog(@"Three->%@",[NSThread currentThread]);
    90     
    91 }
    92 
    93 - (void)didReceiveMemoryWarning {
    94     [super didReceiveMemoryWarning];
    95 
    96 }
    97 
    98 @end

    结果:

  • 相关阅读:
    串口通信
    PS如何将图片/照片转换为线稿
    merge_image.sh
    remk_dir.sh
    简单监控Tomcat连接池大小的命令以及其他简单命令
    一个挖矿病毒的简单发现历程
    IDEA运行tomcat8.5.35源代码
    mybatis 中 if-test 判断大坑
    浅析前端安全:如何防止XSS攻击
    浅析xss攻击原理、模拟xss攻击及防止xss攻击的方法
  • 原文地址:https://www.cnblogs.com/xubaoaichiyu/p/5280712.html
Copyright © 2020-2023  润新知