任何一个 iOS 应用程序都是由一个或者多个线程构成的。无论你是否使用了多线程编程技术,至少有 1 个 线程被创建。多线程就是为了提高引用程序的工作效率!避免阻塞主线程!当我们没有用任何多线程技术的话,默认情况下,是在程序的主线程中执行相关操作!(因此不要在主线程中实现耗时方法)。

比较简单,直接上代码。

 //
// ViewController.m
// CXNSThread
//
// Created by ma c on 16/3/15.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
//
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; /* 常用方法
获取当前线程 NSThread * thread = [NSThread currentThread];
获取主线程 NSThread * main = [NSThread mainThread];
暂停多少秒 [NSThread sleepForTimeInterval:2];
暂停到什么时间段 [NSThread sleepUntilDate:2];
强制停止线程 [NSThread exit];(一旦线程停止 就不能再次开启任务)。 */
/* 线程间的通信
self performSelector:<#(SEL)#> withObject:<#(id)#> withObject:<#(id)#>
self performSelectorOnMainThread:<#(nonnull SEL)#> withObject:<#(nullable id)#> waitUntilDone:<#(BOOL)#>
self performSelector:<#(nonnull SEL)#> onThread:<#(nonnull NSThread *)#> withObject:<#(nullable id)#> waitUntilDone:<#(BOOL)#> */
/* 优点:NSThread比其他两种对线程方案较轻量级,更直观的控制线程对象 缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据加锁会有一定开销。
##(加锁) @synchronized(sel) {
<#statements#>
} ## */ //初始化->动态方法
NSThread * threadOne = [[NSThread alloc]initWithTarget:self selector:@selector(threadOneAction) object:nil]; //设置线程名称
threadOne.name = @"threadOne"; //设置优先级(low ~ height)(0~1)
threadOne.threadPriority = ; //开启线程
[threadOne start]; //静态方法
[NSThread detachNewThreadSelector:@selector(threadTwoAction) toTarget:self withObject:nil]; //隐式创建
[self performSelectorInBackground:@selector(threadThreeAction) withObject:nil]; NSLog(@"Main->%@",[NSThread currentThread]); } -(void)threadOneAction{ //log当前线程
NSLog(@"One->%@",[NSThread currentThread]); } -(void)threadTwoAction{ NSLog(@"Two->%@",[NSThread currentThread]); } -(void)threadThreeAction{ NSLog(@"Three->%@",[NSThread currentThread]); } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; } @end

结果:

IOS NSThread的更多相关文章

  1. [ios]NSThread传值 NSValue传值

    NSThread:http://www.cocoachina.com/bbs/read.php?tid=51873 NSValue:http://blog.sina.com.cn/s/blog_bf9 ...

  2. IOS NSThread 线程间通信

    @interface HMViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @im ...

  3. IOS NSThread(线程同步)

    @interface HMViewController () /** 剩余票数 */ @property (nonatomic, assign) int leftTicketsCount; @prop ...

  4. iOS开发之多线程技术(NSThread、OperationQueue、GCD)

    在前面的博客中如果用到了异步请求的话,也是用到的第三方的东西,没有正儿八经的用过iOS中多线程的东西.其实多线程的东西还是蛮重要的,如果对于之前学过操作系统的小伙伴来说,理解多线程的东西还是比较容易的 ...

  5. iOS GCD NSOperation NSThread等多线程各种举例详解(拷贝)

    2年多的iOS之路匆匆而过,期间也拜读来不少大神的博客,近来突然为自己一直做伸手党感到羞耻,是时候回馈社会.回想当年自己还是小白的时候,照着一些iOS多线程教程学,也只是照抄,只知其然.不知其所以然. ...

  6. IOS 多线程02-pthread 、 NSThread 、GCD 、NSOperationQueue、NSRunLoop

    注:本人是翻译过来,并且加上本人的一点见解. 要点: 1.前言 2.pthread 3.NSThread 4.Grand Central Dispatch(GCD) 5.Operation Queue ...

  7. iOS多线程之NSThread详解

    在iOS中每个进程启动后都会建立一个主线程(UI线程),这个线程是其他线程的父线程.由于iOS中除了主线程,其他子线程是独立于Cocoa Touch的,所以只有主线程可以更新UI界面.iOS多线程的使 ...

  8. iOS开发——多线程篇——NSThread

    一.基本使用1.创建和启动线程一个NSThread对象就代表一条线程 创建.启动线程NSThread *thread = [[NSThread alloc] initWithTarget:self s ...

  9. iOS多线程编程之NSThread的使用

      目录(?)[-] 简介 iOS有三种多线程编程的技术分别是 三种方式的有缺点介绍 NSThread的使用 NSThread 有两种直接创建方式 参数的意义 PS不显式创建线程的方法 下载图片的例子 ...

随机推荐

  1. 【转】在web 项目使用了ReportViewer时出错

    ”应用程序中的服务器错误. -------------------------------------------------------------------------------- 分析器错误 ...

  2. LDPC编译码基本原理

    LDPC编译码基本原理     学习笔记 V1.1 2015/02/18 LDPC编译码基本原理   概述   本文是个人针对LDPC的学习笔记,主要针对LDPC译码算法做了简要的总结.该版本主要致力 ...

  3. [OpenCV] Image Processing - Spatial Filtering

    "利用给定像素周围的像素的值决定此像素的最终的输出值“ 教学效果: 策略: 1. 拉普拉斯,突出小细节: . 梯度,突出边缘: . 平滑过的梯度图像用于掩蔽: . 灰度变换,增加灰度动态范围 ...

  4. 使用ELK(Elasticsearch + Logstash + Kibana) 搭建日志集中分析平台实践--转载

    原文地址:https://wsgzao.github.io/post/elk/ 另外可以参考:https://www.digitalocean.com/community/tutorials/how- ...

  5. Tools Function

    public static void TraceLog(string message, string logFileName) { string tmppath = AppDomain.Current ...

  6. Angular系列----AngularJS入门教程01:AngularJS模板 (转载)

    是时候给这些网页来点动态特性了——用AngularJS!我们这里为后面要加入的控制器添加了一个测试. 一个应用的代码架构有很多种.对于AngularJS应用,我们鼓励使用模型-视图-控制器(MVC)模 ...

  7. P6 EPPM 16 R1 文档和帮助系统

    P6 EPPM 16 R1 文档和帮助系统 https://docs.oracle.com/cd/E74894_01/ http://docs.oracle.com/cd/E68202_01/clie ...

  8. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  9. URL与图像格式

    绝对/相对URL “绝对URL”是指资源的完整的地址,通常以“http://”打头: “相对URL”是指Internet上资源相对于当前页面的地址,它包含从当前页面指向目标资源位置的路径,不以“htt ...

  10. [moka同学笔记]yii2.0表单的使用

    1.创建model   /biaodan.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/08/05 * Tim ...