昨天在项目中使用到了以前所没有使用过的线程,今天有时间来简单的学习一下。

一.线程的创建分为三种方法

  1. (id)init; // designated initializer
  2. (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线程的更多相关文章

  1. IOS 线程处理 子线程

    IOS 线程处理 子线程的启动与结束 技术交流新QQ群:414971585   IOS中,如果要在主线程中启动一个子线程,可以又两种方法: [NSThread detachNewThreadSelec ...

  2. iOS线程之——NSCondition

    多线程在各种编程语言中都是难点,很多语言中实现起来很麻烦,objective-c虽然源于c,但其多线程编程却相当简单,可以与java相媲美.这篇文章主要从线程创建与启动.线程的同步与锁.线程的交互.线 ...

  3. iOS 线程操作库 PromiseKit

    iOS 线程操作库 PromiseKit 官网:http://promisekit.org/ github:https://github.com/mxcl/PromiseKit/tree/master ...

  4. ios线程和GCD

    1.什么是进程? 进程是指在系统中正在运行的一个应用程序.比如同时打开QQ.Xcode,系统就会分别启动2个进程.截图 2.什么是线程? 1).一个进程要想执行任务,必须得有线程(每一个进程至少要有一 ...

  5. iOS - 线程管理

    iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...

  6. IOS线程的一些总结

    主线程的作用 (在主线程中才能设置) 显示/刷新UI界面 处理UI事件(比如点击事件.滚动事件.拖拽事件): 主线程的使用注意 别将比较耗时的操作放到主线程中. 耗时操作会卡住主线程.影响体验. [N ...

  7. iOS 线程锁同步机制

    转载自 http://yulingtianxia.com/blog/2015/11/01/More-than-you-want-to-know-about-synchronized/ 如果你已经使用 ...

  8. IOS线程操作(3)

    采用CGD更有效的比前两个(它被认为是如此,有兴趣的同学可以去试试). 这是推荐的方式来使用苹果的比较. GCD它是Grand Central Dispatch缩写,这是一组并行编程C介面. GCD是 ...

  9. ios线程和GCD和队列同步异步的关系

    1.什么是进程? 进程是指在系统中正在运行的一个应用程序.比如同时打开QQ.Xcode,系统就会分别启动2个进程.截图 2.什么是线程? 1).一个进程要想执行任务,必须得有线程(每一个进程至少要有一 ...

  10. iOS 线程安全--锁

    一,前言 线程安全是iOS开发中避免了的话题,随着多线程的使用,对于资源的竞争以及数据的操作都可能存在风险,所以有必要在操作时保证线程安全. 二,为什么要使用锁? 由于一个进程中不可避免的存在多线程, ...

随机推荐

  1. Selenium Grid 运行报错 Exception thrown in Navigator.Start first time ->Error forwarding the new session Empty pool of VM for setup Capabilities

    Selenium Grid 运行报错 : Exception thrown in Navigator.Start first time ->Error forwarding the new se ...

  2. java 集合(Set2)

    TreeSet: 1.向TreeSet添加元素时 如果元素具有自然特性,那么就按照元素的自然顺序的特点进行排序储存. 如果不具备,就要实现Compareable接口中的compareTo() 方法. ...

  3. 由函数clock想到的

    今天介绍一下clock这个函数的使用,它是C标准库的一部分,声明在头文件<time.h>中,返回处理器使用的时间值,函数声明为: clock_t clock(void); 这个函数看起来很 ...

  4. 【CITE】DrawImage方法详解(转)

    Image和Bitmap类概述 GDI+的Image类封装了对BMP.GIF.JPEG.PNG.TIFF.WMF(Windows元文件)和EMF(增强WMF)图像文件的调入.格式转换以及简单处理的功能 ...

  5. DSP算法学习-过采样技术

    DSP算法学习-过采样技术 彭会锋 2015-04-27 23:23:47 参考论文: 1 http://wr.lib.tsinghua.edu.cn/sites/default/files/1207 ...

  6. 转 SQL Union和SQL Union All两者用法区别效率以及与order by 和 group by配合问题

    SQL Union和SQL Union All两者用法区别效率以及与order by 和 group by配合问题 SQL Union和SQL Union All用法 SQL UNION 操作符 UN ...

  7. Caffe 深度学习框架介绍

    转自:http://suanfazu.com/t/caffe/281 Caffe是一个清晰而高效的深度学习框架,其作者是博士毕业于UC Berkeley的贾扬清,目前在Google工作. Caffe是 ...

  8. [转]C#取得内网IP、外网IP、客户端IP方法

    前言 在 Windows Form Application 里对于取得 IP Address 有内网.外网两种 IP Address ,如果只需要取得内网 IP Address ,可以透过使用 IPH ...

  9. Window["aaa"]这个在JS里是什么意思?

    答案:定义一个全局的变量 aaa,这个的方式是数组,实际上是等于 window.aaa

  10. qt--- vs

    qt with vs 1.安装vs2012: 2.下载Qt 5.2.0 for Windows 32-bit (VS 2012, 579 MB) 和 Visual Studio Add-in 1.2. ...