iOS线程
昨天在项目中使用到了以前所没有使用过的线程,今天有时间来简单的学习一下。
一.线程的创建分为三种方法
- (id)init; // designated initializer
- (id)initWithTarget:(id)target selector: (SEL)selector object:(id)argument;
以上两种方法创建的线程方法,需要另外调用 “start”方法来启动该线程
3.(void)detachNewThreadSelector: (SEL)aSelector toTarget: (id)aTarget withObject: (id)anArgument
而上面这种创建线程的方法,则不需要再额外调用其他方法来启动线程。
4. [selfperformSelectorInBackground:@selector(threadAction) withObject:self];
也不用调用启动方法。更多的使用县城方法,倾向于使用这种方式。
5.NSOperationQueue *operationQueue = [[NSOperationQueuealloc]init];
[operationQueue addOperationWithBlock:^{
for (int i = 0; i < 30; i++) {
NSLog(@"多线程:%d",i);
}
}];
6.
//1.线程池:这个对象可设置线程执行优先级和线程并发数(同时段执行多少线程)
NSOperationQueue *operationQueue = [[NSOperationQueuealloc]init];
[operationQueue addOperationWithBlock:^{
for (int i = 0; i < 30; i++) {
NSLog(@"多线程:%d",i);
}
}];
//设置并发数为1,线程池中同一时间只能执行一个线程
operationQueue.maxConcurrentOperationCount = 1;
NSInvocationOperation *operation1 = [[NSInvocationOperationalloc] initWithTarget:selfselector:@selector(operationAction1) object:nil];
//设置线程的优先级
[operation1 setQueuePriority:NSOperationQueuePriorityLow];
NSInvocationOperation *operation2 = [[NSInvocationOperationalloc] initWithTarget:selfselector:@selector(operationAction1) object:nil];
[operation2 setQueuePriority:NSOperationQueuePriorityHigh];
//把线程添加进线程池中去
[operationQueue addOperation:operation1];
[operationQueue addOperation:operation2];
二:1。暂时遇到一个说法,更新UI必须得回到主线程。
暂时我不清楚这样的原因,并且什么情况,或者说怎么使用线程方法才是回到主线程,对于我来说还是一个迷。
2。清楚的是,调用以下这个方法是回到主线程。(下面这个方法,也在很多情况下被用到,以此来回到主线程来更新UI)
用NSObject的类方法 performSelectorInBackground:withObject: 创建一个线程:
[Obj performSelectorInBackground:@selector(doSomething) withObject:nil];
三:下面这一段代码或许就是对问题二得一个合理得解释。利用一中得三类方法开得线程都不是主线程,所以在下面代码中,新开得线程方法里下载完图片之后,需要返回到主线程来更新UI。
//
// ViewController.m
// NSThreadDemo
//
// Created by rongfzh on 12-9-23.
// Copyright (c) 2012年 rongfzh. All rights reserved.
//
#import "ViewController.h"
#define kURL @"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"
@interface ViewController ()
@end
@implementation ViewController
-(void)downloadImage:(NSString *) url{
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [[UIImage alloc]initWithData:data];
if(image == nil){
}else{
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
}
}
-(void)updateUI:(UIImage*) image{
self.imageView.image = image;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImage:) object:kURL];
[thread start];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
iOS线程的更多相关文章
- IOS 线程处理 子线程
IOS 线程处理 子线程的启动与结束 技术交流新QQ群:414971585 IOS中,如果要在主线程中启动一个子线程,可以又两种方法: [NSThread detachNewThreadSelec ...
- iOS线程之——NSCondition
多线程在各种编程语言中都是难点,很多语言中实现起来很麻烦,objective-c虽然源于c,但其多线程编程却相当简单,可以与java相媲美.这篇文章主要从线程创建与启动.线程的同步与锁.线程的交互.线 ...
- iOS 线程操作库 PromiseKit
iOS 线程操作库 PromiseKit 官网:http://promisekit.org/ github:https://github.com/mxcl/PromiseKit/tree/master ...
- ios线程和GCD
1.什么是进程? 进程是指在系统中正在运行的一个应用程序.比如同时打开QQ.Xcode,系统就会分别启动2个进程.截图 2.什么是线程? 1).一个进程要想执行任务,必须得有线程(每一个进程至少要有一 ...
- iOS - 线程管理
iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...
- IOS线程的一些总结
主线程的作用 (在主线程中才能设置) 显示/刷新UI界面 处理UI事件(比如点击事件.滚动事件.拖拽事件): 主线程的使用注意 别将比较耗时的操作放到主线程中. 耗时操作会卡住主线程.影响体验. [N ...
- iOS 线程锁同步机制
转载自 http://yulingtianxia.com/blog/2015/11/01/More-than-you-want-to-know-about-synchronized/ 如果你已经使用 ...
- IOS线程操作(3)
采用CGD更有效的比前两个(它被认为是如此,有兴趣的同学可以去试试). 这是推荐的方式来使用苹果的比较. GCD它是Grand Central Dispatch缩写,这是一组并行编程C介面. GCD是 ...
- ios线程和GCD和队列同步异步的关系
1.什么是进程? 进程是指在系统中正在运行的一个应用程序.比如同时打开QQ.Xcode,系统就会分别启动2个进程.截图 2.什么是线程? 1).一个进程要想执行任务,必须得有线程(每一个进程至少要有一 ...
- iOS 线程安全--锁
一,前言 线程安全是iOS开发中避免了的话题,随着多线程的使用,对于资源的竞争以及数据的操作都可能存在风险,所以有必要在操作时保证线程安全. 二,为什么要使用锁? 由于一个进程中不可避免的存在多线程, ...
随机推荐
- C语言中的转义字符
转义字符 意义 ASCII码值(十进制) \a 响铃(BEL) 007 \b 退格(BS) ,将当前位置移到前一列 008 \f 换页(FF),将当前位置移到下页开头 012 \n 换行(LF) ,将 ...
- vc++编译libtiff4.0.4
目录 第1章简介 1 第2章命令行编译 2 2.1 编译 2 2.1.1 使用VC++2010编译 2 2.1.2 使用VC++6编译 4 2.2 生成的文件 5 ...
- discuz核心类库class_core的函数注释
class discuz_core { // 数据库存储引擎 var $db = null; // 内存缓冲object var $mem = null; // 会话 object var $sess ...
- 172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- ABAP简单表维护的制作
为了知识的积累,特作了个简单的表维护. 因为自己之前做dynpro程序的时候建了一个Tree node的表,所以就不在此重复.(在表的交付和维护页签中标的属性要是‘允许标准表维护的’) 直接Alt+U ...
- backbonejs中的模型篇(一)
一:模型及属性 模型是MVC应用的基石,它负责存放应用所需的数据,对数据的验证,执行访问控制,以及实现应用所需的特定业务逻辑. backbone通过扩展Backbone.Model对象来定义一个模型. ...
- 216. Combination Sum III——本质DFS
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- struts2拦截器与过滤器
转载:http://www.cnblogs.com/JohnLiang/archive/2011/12/15/2288376.html 过滤器,是在java web中,你传入的request,resp ...
- java网络编程socket解析
转载:http://www.blogjava.net/landon/archive/2013/07/02/401137.html Java网络编程精解笔记2:Socket详解 Socket用法详解 在 ...
- Eclipse如何设置代码提示功能
Windows→Preference→XML→XML Files→Editor→Content Assist→Auto Activation→Prompt when these characters ...