IOS线程学习(一)

1.NSThread 

官方的描述

An NSThread object controls a thread of execution. Use this class when you want to have an Objective-C method run in its own thread of execution. Threads are especially useful when you need to perform a lengthy task, but don’t want it to block the execution of the rest of the application.

NSThread能控制一个线程的执行, 当你想在自己的线程执行OC方法时请用此类。对于执行较长的任务时这是也很有用的,不会堵住程序里面剩下需要执行的任务。

 NSThread *thread = [[NSThread alloc]initWithTarget:self selector:NSSelectorFromString(@"myThread:") object:nil];
 //启动线程
 [thread start];//停止线程//if (![thread isCancelled]) {
//[thread cancel];

//    }

 1 -(void)myThread:(id)sender{
 2     NSLog(@"%@" , sender);
 3     @synchronized(self){
 4         while (true) {
 5             [NSThread sleepForTimeInterval:1];
 6             static int i = 0;
 7             NSLog(@"%i" , i++);
 8             if (i == 10) {
 9                 [NSThread exit];
10             }
11         }
12     }
13 }

结果:只打印到3时线程就终止了

也可以用这个方法启动一个线程,但是不能是默认的Thread配置

[NSThread detachNewThreadSelector:NSSelectorFromString(@"myThread:") toTarget:self withObject:@"myThread"];

也等同于,这个方法在NSObject中被定义,只要是继承NSObject都可以这样用

[self performSelectorInBackground:NSSelectorFromString(@"myThread:") withObject:@"myThread"];

 2.NSOperation

目前我的理解就是一个封装操作某操作的,然后调用其start方法,就在主线程执行!!!

如果不在主线程执行可以创建一个NSOperationQueue,然后将操作加入到其中执行

其有两个子类NSBlockOperation和NSInvocationOperation

NSBlockOperation

The NSBlockOperation class is a concrete subclass of NSOperation that manages the concurrent execution of one or more blocks. You can use this object to execute several blocks at once without having to create separate operation objects for each. When executing more than one block, the operation itself is considered finished only when all blocks have finished executing.

可见,NSBlockOperation是管理多个Block块的,而且只有所有的Block都执行完了才会变成finished状态;

    NSLog(@"%@ mainT = " ,[NSThread currentThread]);

    NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i<5; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"block1>>>%i thread = %@" , i , [NSThread currentThread]);
        }
    }];

    [blockOp addExecutionBlock:^{
        for (int i = 0; i<10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"block2>>>%i thread = %@" , i , [NSThread currentThread]);
        }
    }];

    [blockOp start];

    NSLog(@"到这了");

运行结果:一个Block就在主线程,多个就会并行执行其他block

NSInvocationOperation

The NSInvocationOperation class is a concrete subclass of NSOperation that manages the execution of a single encapsulated task specified as an invocation. You can use this class to initiate an operation that consists of invoking a selector on a specified object. This class implements a non-concurrent operation.

可见,只能通过Action-Target模式加入一个操作

      NSLog(@"%@ mainThread = " ,[NSThread currentThread]);
    NSInvocationOperation *invocationOperation1= [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(myOperation:) object:@"invocationOperation1"];

    [invocationOperation1 start];

     NSLog(@"到这了");

-(void)myOperation:(id)sender{
        static int i = 0;
        while (i<4) {
            [NSThread sleepForTimeInterval:1];
            [NSThread isMainThread];
            NSLog(@"我是线程%@ %i", [NSThread currentThread] , i++);
        }
}

运行结果:

2015-12-19 22:57:23.038 MYThread[4942:44405] <NSThread: 0x7ffcba50c190>{number = 1, name = main} mainThread =
2015-12-19 22:57:24.043 MYThread[4942:44405] 我是线程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 0
2015-12-19 22:57:25.046 MYThread[4942:44405] 我是线程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 1
2015-12-19 22:57:26.051 MYThread[4942:44405] 我是线程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 2
2015-12-19 22:57:27.053 MYThread[4942:44405] 我是线程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 3
2015-12-19 22:57:27.054 MYThread[4942:44405] 到这了
时间: 2024-08-11 02:12:35

IOS线程学习(一)的相关文章

ios线程学习

1.什么是进程? 进程是指在系统中正在运行的一个应用程序.比如同时打开QQ.Xcode,系统就会分别启动2个进程.截图 2.什么是线程? 1).一个进程要想执行任务,必须得有线程(每一个进程至少要有一条线程) 2).线程是进程的基本执行单元,一个进程(程序)的所有任务都在线程中执行 3).一个线程中任务的执行是串行的.即如果要在1个线程中执行多个任务,那么只能一个一个的按顺序执行这些任务 3.什么是多线程? 1).一个线程中可以开启多条线程,每条线程可以并行执行不同的任务.比如同时开启三条线程分

开源中国iOS客户端学习

开源中国iOS客户端学习 续写前言 <开源中国iOS客户端学习>续写前系列博客    http://blog.csdn.net/column/details/xfzl-kykhd.html 开源中国iOS客户端学习——序 说到这款开源软件就得提到她的娘家了--开源中国社区: 开源中国社区简介:开源中国 www.oschina.net 成立于2008年8月,是目前中国最大的开源技术社区.传播开源的理念,推广开源项目,为 IT 开发者提供了一个发现.使用.并交流开源技术的平台.目前开源中国社区已收

iOS从零开始学习socket编程——高并发多线程服务器

在上一篇文章<iOS从零开始学习socket编程--HTTP1.0服务器端>中我们已经简单的接触了OC搭建的HTTP服务器. (地址http://blog.csdn.net/abc649395594/article/details/45131373) 出于用户体验和鲁棒性考虑,这里把这个HTTP服务器改进成多线程的. 首先,AnsycSocket这个类是基于OC的Runloop实现的,Runloop实现了方法的异步调用但并不支持多线程. 在这里首先简单区分一下多线程和方法异步调用的区别.他们都

iOS GCD学习

GCD概念:Grand Central Dispatch 1.queue 队列 dispatch queue 分发队列 2.task  任务 3.thread 线程 task 是一个一个单独的任务(方法,函数,block) queue里存放的是一个或者多个task thread 为了保证task能顺利执行,queue会开辟适当的thread,在开辟的thread里面执行task queue分两种: 1.serialqueue(串行队列) 特点:任务先进先出.排在前面任务先执行,执行之后后面的任务

iOS入门学习详解

本文来源:http://www.zretc.com/technologyDetail/447.html ios学习起来并没有想象中的那么困难,只不过是你想要开发ios的话,成本相对比较高吧,在windows里面装虚拟机的时代我是体验过的,那个开发起来简直就是苦不堪言啊,那个宕机的速度真是分分钟的事,reset已经被用的如火纯清了,这样你就需要一台mac,有了mac还不够,总不能全部用模拟器开发吧,像定位啊之类的,还是需要实机来测试的,模拟器跟实机的差别还是挺大的.当然也没必要开发之前就买一个帐号

ios学习笔记---ios完整学习路线

ios完整学习路线

ios开发学习资料总汇

ios开发学习资料总汇 下面是收集的一些学习资料. 1.唐巧精心整理了国内40多位iOS开发博主的博客地址列表 2.ios常见加密: 链接: http://pan.baidu.com/s/1eQTGFIE 密码: p8ay 3.

【iOS知识学习】_iOS动态改变TableView Cell高度

在做tableView的时候,我们有时候需要根据cell的高度动态来调整,最近在网上看到一段代码不错,跟大家Share一下. 在 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 类中获取cell的高度: CGSize boundSize = CGSizeMake(216, CGFLOAT_MAX); cell.textLabel.text

IOS开发学习笔记-(2)键盘控制,键盘类型设置,alert 对话框

一.关闭键盘,放弃第一响应者,处理思路有两种 ① 使用文本框的 Did End on Exit 绑定事件 ② UIControl on Touch 事件 都去操作 sender 的  resignFirstResponder #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *txtUserName; @pro