zoukankan      html  css  js  c++  java
  • iOS--多线程

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        // iOS下的多线程
        // 1. 进程和线程
        // 什么是进程? 进程是当用户下达执行应用程序的命令时, 所启动的东西.
        // 什么是线程? 线程是进程的执行单位, 线程比作工人, 进程比作工厂
        // 2. 多线程
        // 为了更好的进行一些程序的操作, 节省时间, 提高效率.
        // 一个进程不能过多的创建线程, 资源消耗过大.(工厂不能有太多的工人, 工资发不起) 合理的是:开辟5个左右的线程
        // 注意: 所有 UI 的操作都放在主线程中进行
        // 需要开辟线程的耗时操作: 图像文字渲染, 大数据存储, 网络请求等.
        
        // 3. NSThread
        // 1> currentThread 获取当前线程
        //    <NSThread: 0x7f93da406fb0>{number = 1, name = main}
        //         name: 线程名    number: 线程编号
        //    main 代表主线程
        NSThread *thread = [NSThread currentThread];
        // 2> 开辟一个子线程执行操作
        //    子线程随机创建
        [NSThread detachNewThreadSelector:@selector(newThread) toTarget:self withObject:nil];
        // 3> 延迟当前线程的执行, 固定日期
        NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
        // [NSThread sleepUntilDate:date];
        NSLog(@"1111");
        // 4> 延迟当前线程的执行, 相对时间
        [NSThread sleepForTimeInterval:1];
        NSLog(@"1111");
        // 5> 退出当前线程, 不要退出主线程
        //    退出后, 线程后续操作不会再执行
        // [NSThread exit];
        NSLog(@"%@", [NSThread mainThread]);
        // 6> threadPriority 线程优先级
        //    默认优先级是 0.5
        double priority = [NSThread threadPriority];
        NSLog(@"%lf", priority);
        // 7> name 线程名
        [NSThread mainThread].name = @"马峰的主线程";
        NSLog(@"%@", thread);
        // 8> isMainThread 判断是否是主线程
        // 判断当前线程
        [NSThread isMainThread];
        // 判断线程对象是不是主线程
        [[NSThread mainThread] isMainThread];
        
        // 9> 初始化方法  需要手动管理线程的生命周期
        // 初始化一个未启动的线程
      //  NSThread *newThread = [[NSThread alloc] initWithTarget:self selector:@selector(myThread) object:nil];
      //   [newThread start];
        
       // _imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://bizhi.zhuoku.com/2016/08/02/bing/bing06.jpg"]]];
        // 10> 将耗时的操作放到后台, 不关心线程问题
        [self performSelectorInBackground:@selector(myThread) withObject:nil];
        // 11> performSelectorOnMainThread 返回主线程执行操作
        
        
        
    }
    
    - (void)myThread {
        NSLog(@"%s", __FUNCTION__);
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic1.win4000.com/wallpaper/8/511ce1c9ee276.jpg"]];
        // 返回主线程显示图片
        [self performSelectorOnMainThread:@selector(onMainThreadHandleImage:) withObject:data waitUntilDone:YES];
    }
    
    - (void)onMainThreadHandleImage:(id)object {
        _imageView.image = [UIImage imageWithData:object];
    }
    
    - (void)newThread {
        
        NSThread *thread = [NSThread currentThread];
        thread.name = @"xxx 的线程";
        double priority = [NSThread threadPriority];
        NSLog(@"%lf", priority);
        NSLog(@"%@", thread);
        
    }
    
    - (IBAction)click:(id)sender {
        for (int i = 0; i < 100000000; i++) {
            NSLog(@"循环了%d", i);
        }
       
    }
    
  • 相关阅读:
    Java 8新特性探究(九)跟OOM:Permgen说再见吧
    Java 堆内存(Heap)[转]
    hashmap源码
    Java8学习笔记----Lambda表达式 (转)
    双重检查锁定与延迟初始化(转自infoq)
    kvm的live-snapshot
    8.25考试总结
    CF1151FSonya and Informatics
    CF1151div2(Round 553)
    BZOJ3728 PA2014Final Zarowki
  • 原文地址:https://www.cnblogs.com/mafeng/p/5781021.html
Copyright © 2011-2022 走看看