NSThread多线程

  1 //
  2 //  ViewController.m
  3 //  nsthreaddemo
  4 //
  5 //  Created by ys on 15/11/23.
  6 //  Copyright (c) 2015年 ys. All rights reserved.
  7 //
  8 //需求分析
  9 //
 10 //1. UIImageView显示图片
 11 //2. UIImage模拟从网络上下载
 12 //3. NSString记录图片路径
 13 #import "ViewController.h"
 14
 15 @interface ViewController ()
 16
 17 @property (nonatomic, strong) UIImageView *imageView;
 18 @property (nonatomic, strong) UIImage *image;
 19 @property (nonatomic, strong) NSString *imagePath;
 20
 21 @end
 22
 23 @implementation ViewController
 24
 25 // 0. 模拟使用图像路径加载图片
 26 - (void)setImagePath:(NSString *)imagePath
 27 {
 28     @autoreleasepool {
 29         NSLog(@"%@", [NSThread currentThread]);
 30
 31         // 1> 模拟下载,延时
 32         [NSThread sleepForTimeInterval:1.0];
 33
 34         // 2> 设置图像,苹果底层允许使用performSelectorInBackground方法
 35         // 在后台线程更新UI,强烈不建议大家这么做!
 36         // YES会阻塞住线程,直到调用方法完成
 37         // NO不会阻塞线程,会继续执行
 38         [self performSelectorOnMainThread:@selector(setImage:) withObject:[UIImage imageNamed:imagePath] waitUntilDone:NO];
 39     }
 40 }
 41
 42 // 1. 图像
 43 - (void)setImage:(UIImage *)image
 44 {
 45
 46     self.imageView.image = image;
 47
 48     //    [NSThread sleepForTimeInterval:1.0];
 49     // 根据图片自动调整大小
 50     [self.imageView sizeToFit];
 51 }
 52
 53 // 2. 创建imageView
 54 - (UIImageView *)imageView
 55 {
 56     if (!_imageView) {
 57         _imageView = [[UIImageView alloc] init];
 58     }
 59
 60     return _imageView;
 61 }
 62
 63
 64 - (void)viewDidLoad {
 65     [super viewDidLoad];
 66     [self.view addSubview:self.imageView];
 67
 68     // 在后台线程执行这段代码即可
 69     for (int i = 0; i < 50; ++i) {//打印线程会发现新建了大量线程
 70         [self performSelectorInBackground:@selector(setImagePath:) withObject:@"头像1.png"];
 71     }
 72 }
 73
 74 //
 75 //小结
 76 //
 77 //方法,看起来很简单,
 78 //
 79 //1> 不能够自动回收线程,如果并发数量多,会建立大量的子线程!
 80 //2> 使用NSThread的线程,不会自动添加autoreleasepool
 81 //
 82 //意味着,如果在后台线程方法中,
 83 //
 84 //
 85 //主线程中是有自动释放池的,使用GCD和NSOperation也会自动添加自动释放池
 86 //
 87 //NSThread和NSObject不会,如果在后台线程中创建了autorelease的对象,需要使用自动释放池,否则会出现内存泄漏!
 88 //
 89 //@autoreleasepool {} 自动释放池
 90 //工作原理:
 91 //
 92 //1. 当自动释放池被销毁或者“耗尽”时,对池中的所有对象发送release消息,清空自动释放池
 93 //2. 所有autorelease的对象,在出了作用域之后,会自动添加到【最近一次创建的自动释放池中】自动释放池中
 94 //
 95 //在ARC中,编译器在编译过程中,会自动根据代码结构,添加retain和release。
 96 @end
 97
 98 //- (void)demo
 99 //{
100 //    // 提问:代码存在什么问题?如果循环次数非常大,会出现什么问题?应该如何修改?
101 //
102 //    // 解决办法1:如果i比较大,可以在for循环外添加自动释放池
103 //    // 解决方法2:如果i玩命大,一次循环都会造成自动释放池被填满,可以在for循环内添加自动释放池
104 //    for (int i = 0; i < 10000000; ++i) {
105 //        @autoreleasepool {
106 //            // *
107 //            NSString *str = @"Hello World!";
108 //            // new *
109 //            str = [str uppercaseString];
110 //            // new *
111 //            str = [NSString stringWithFormat:@"%@ %d", str, i];
112 //
113 //            NSLog(@"%@", str);
114 //        }
115 //    }
116 //}
时间: 2024-08-24 13:09:11

NSThread多线程的相关文章

NSThread多线程总结(一)

一: NSThread 小节 //1. object是传值的对象 创建完成后 自动启动 [NSThread detachNewThreadSelector:@selector(download:) toTarget:self withObject:@"http:"]; //2. 第二种方式 隐事创建  也是自动启动 [self performSelectorInBackground:@selector(download:) withObject:nil];    //3. 一个NSTh

iOS-有限缓冲问题经典案例-生产者消费者-NSThread多线程

1 #import "ViewController.h" 2 3 @interface ViewController () 4 { 5 NSMutableArray *_arr; 6 NSCondition *_condition; 7 } 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 15 _arr = [NSMutableArray

NSThread 多线程 三种方式

// //  ZBMainViewController.m //  TestProject // //  Created by 张先森 on 14/12/5. //  Copyright (c) 2014年 zhibin. All rights reserved. // #import "ZBMainViewController.h" @interface ZBMainViewController () @property(nonatomic,strong)CALayer *mylay

NSThread 多线程相关

1.下面的代码,有2点需要注意,1>就是 就是thread:所传得参数,这里传得的是nsarray 当然也可以传其他的类型.2> [self performSelectorOnMainThread:@selector(update) withObject:nil waitUntilDone:YES]; 这个函数的作用是通知主线程进行一下操作,比如这里是更新btn 的title.主要的参数需要注意的是 waitUntilDone ,如果是YES 那就需要等到 update操作完之后才会执行NSL

iOS开发:NSThread多线程的使用

一:创建线程,NSThread创建线程常用的三种方式: //1:手动创建启动 let thread:NSThread = NSThread(target: self, selector:"doSomething:", object: "param") thread.name = "childThread1" //手动创建线程的方式可以设置线程的名称 thread.start() //2:创建完成自动start NSThread.detachNew

iOS中多线程_05_线程间通信NSThread/GCD

1.什么叫做线程间通信 在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信 2.线程间通信的体现 1个线程传递数据给另1个线程 在1个线程中执行完特定任务后,转到另1个线程继续执行任务 3.线程间通信示例 UIImageView下载图片这个例子, 主线程中开启一个子线程去下载图片, 当图片下载完成之后再回到主线程中更新显示图片, 这样的一个过程就是线程间通信的一个过程. 4.NSThread线程间通信常用方法 // 第一种- (void)performSelectorOnMain

iOS开发多线程网络———NSThread

@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); iOS开发多线程网络———NS

iOS开发——多线程OC篇&amp;多线程详解

多线程详解 前面介绍了多线程的各种方式及其使用,这里补一点关于多线程的概念及相关技巧与使用,相信前面不懂的地方看了这里之后你就对多线程基本上没有什么问题了! 1——首先ios开发多线程中必须了解的概念: 进程 正在进行中的程序被称为进程,负责程序运行的内存分配 每一个进程都有自己独立的虚拟内存空间 线程 线程是进程中一个独立的执行路径(控制单元) 一个进程中至少包含一条线程,即主线程 可以将耗时的执行路径(如:网络请求)放在其他线程中执行 创建线程的目的就是为了开启一条新的执行路径,运行指定的代

iOS开发 - 多线程

知识点 1.理解线程的概念 2.NSThread的使用 3.NSOperation的使用 4.GCD的使用 5.线程锁,线程安全 =============================== 1.多线程是一种实现多任务并发执行的技术,允许同时执行多个任务,能够更合理的利用CPU的资源,提高效率.防止用户界面卡顿. 在iOS中,所有的UI处理只能在主线程做. 什么是进程? · 简单的说进程就是我们电脑上运行的一个个应用程序,每一个程序就是一个进程,并且每个进程之间是独立的,每个进程运行在其专用受