任何一个 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. GET方法传递中文参数乱码解决办法

    1.在页面中对你的URL进行编码 使用------encodeURI(你要使用的中文参数值)如:...?username"+encodeURI(“小甜甜") 2.在后台通过解码来接 ...

  2. maven -- 问题解决(一)解决eclipse中maven项目出现的问题

    配置项目时出现的错误: error: Cannot change version of project facet Dynamic Web Module to 2.5. error: One or m ...

  3. Android 布局之LinearLayout

    Android 布局之LinearLayout 1 LinearLayout简介 LinearLayout是线程布局.它包括2个方向(android:orientation):“水平”(horizon ...

  4. 笔试测试开发题三道(python)

    笔试遇到的三道测试开发题,虽然都不难,但关键还是思路吧!我想在开发东西的时候应该具备的就是思路,有了思路尝试去写,或查相关文档或代码,在此基础上需要不断调整最终达到需求.思路又是在不断练习中获得的. ...

  5. [Altera] Device Part Number Format

    大体可分为五个部分,不排除有特例. 第一部分说明器件归属的器件系列, 第二部分说明器件的封装类型, 第三部分说明器件的引脚数目, 第四部分说明器件的工作温度范围, 第五部分说明器件的速度等级. 实践中 ...

  6. Windows平台下ActiveMQ 安装

    安装之前需要先确定机器上已经有JVM环境,如果没有则会在安装过程中提示 Unable to execute Java command.  系统找不到指定的文件 第一步:从官网下载ActiveMQ的安装 ...

  7. Scrum1.2--spring计划

    项目功能--深入分析  燃尽图

  8. 用Qt写软件系列二:QCookieViewer(浏览器Cookie查看器)

    预备 继上篇<浏览器缓存查看器QCacheViewer>之后,本篇开始QCookieViewer的编写.Cookie技术作为网站收集用户隐私信息.分析用户偏好的一种手段,广泛应用于各大网站 ...

  9. ok6410 android driver(3)

    This article discusses the Makefile and how to port the module to different platform (localhost and ...

  10. NetworkComms.Net github下载地址

    https://github.com/MarcFletcher/NetworkComms.Net