//
// ViewController.m
// 03-掌握-NSThread基本使用 #import "ViewController.h"
#import "XMGThread.h" @interface ViewController () @end @implementation ViewController -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self createNewThread1];
} //1.alloc init 创建线程,需要手动启动线程,根据线程的number判断是不是同一个线程。
//线程的生命周期:当任务执行完毕之后被释放掉(比较特殊,)
-(void)createNewThread1
{
//1.创建线程
/* 第一个参数:目标对象 self
第二个参数:方法选择器 调用的方法
第三个参数:前面调用方法需要传递的参数 nil */ //XMGThread是继承自NSThread的类,threadA是局部变量,大括号之后就销毁了,
XMGThread *threadA = [[XMGThread alloc]initWithTarget:self selector:@selector(run:) object:@"ABC"];
//设置属性
threadA.name = @"线程A";//属性用点语法和set方法是一样的。
//设置优先级 取值范围 0.0 ~ 1.0 之间 最高是1.0 默认优先级是0.5,获取cpu的几率更大,
threadA.threadPriority = 1.0;
//2.启动线程,// 进入就绪状态 -> 运行状态。当线程任务执行完毕,自动进入死亡状态
[threadA start]; NSThread *threadB = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"ABC"];//object是run方法的参数,处于暂停状态。
threadB.name = @"线程b";
threadB.threadPriority = 0.1;
[threadB start]; NSThread *threadC = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"ABC"];
threadC.name = @"线程C";
[threadC start]; [self createNewThread3]; } //2.分离子线程,自动启动线程,不需要start,拿不到线程对象,无法对线程进行更详细的设置
-(void)createNewThread2
{
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"分离子线程"];
} //3.开启一条后台线程,不需要start,拿不到线程对象,无法对线程进行更详细的设置
-(void)createNewThread3
{
[self performSelectorInBackground:@selector(run:) withObject:@"开启后台线程"];
} -(void)run:(NSString *)param
{
NSLog(@"---run----%@---%@",[NSThread currentThread].name,param);
for (NSInteger i = ; i<; i++) {
NSLog(@"%zd----%@",i,[NSThread currentThread].name);
}
} @end /*
启动线程
- (void)start;
// 进入就绪状态 -> 运行状态。当线程任务执行完毕,自动进入死亡状态 阻塞(暂停)线程
+ (void)sleepUntilDate:(NSDate *)date;
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;
// 进入阻塞状态 强制停止线程
+ (void)exit;
// 进入死亡状态 注意:一旦线程停止(死亡)了,就不能再次开启任务
*/
//
// XMGThread.h
// 03-掌握-NSThread基本使用
//
// Created by xiaomage on 16/2/18.
// Copyright © 2016年 小码哥. All rights reserved.
// #import <Foundation/Foundation.h> @interface XMGThread : NSThread @end
//
// XMGThread.m
// 03-掌握-NSThread基本使用
//
// Created by xiaomage on 16/2/18.
// Copyright © 2016年 小码哥. All rights reserved.
// #import "XMGThread.h" @implementation XMGThread -(void)dealloc
{
NSLog(@"dealloc----%@",[NSThread currentThread]); //线程的run方法执行完之后,才调用。
}
@end

ios31--NSThread的更多相关文章

  1. 4.1/4.2 多线程进阶篇<上>(Pthread & NSThread)

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 本文源码 Demo 详见 Githubhttps://github.com/shorfng ...

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

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

  3. iOS多线程之3.NSThread的线程间通信

      我们把一些耗时操作放在子线程,例如下载图片,但是下载完毕我们不能在子线程更新UI,因为只有主线程才可以更新UI和处理用户的触摸事件,否则程序会崩溃.此时,我们就需要把子线程下载完毕的数据传递到主线 ...

  4. iOS多线程之2.NSThread的加锁@synchronized

    我在上一篇文章讲了线程的生命周期,这篇文章来讲讲线程加锁的注意事项与@synchronized关键字.   那什么时候需要加锁呢,就是当多条线程同时操作一个变量时,就需要加锁了.至于为什么要加锁,可以 ...

  5. 1.多线程-NSThread

    1.在主线程执行多次NSLog模拟耗时操作 结果,卡住主线程 解决方案: performSelectorInBackground让程序在后台执行   2.pthread的使用 开辟子线程,执行一个函数 ...

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

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

  7. IOS UI多线程 NSThread 下载并显示图片到UIImageView

    效果图 @property (weak,nonatomic)IBOutletUILabel *downLabelInfo; @property (weak,nonatomic)IBOutletUIIm ...

  8. NSThread 子线程 Cocoa NSOperation GCD(Grand Central Dispatch) 多线程

    单词:thread 英 θred:n 线.思路.vt 穿过.vi 穿透过 一.    进程.线程 进程:正在进行中的程序被称为进程,负责程序运行的内存分配,每一个进程都有自己独立的虚拟内存空间 线程: ...

  9. 多线程NSThread基本用法

        #import "ViewController.h" @interface ViewController () @end @implementation ViewContr ...

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

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

随机推荐

  1. vsftp虚拟用户方式访问

    需求:外部人员需要对公司服务器上某个文件夹内容进行读写操作 文件目录信息:/opt/abc drwxr-xr-x 9 www  www       4096 12月  4 13:02 abc   #注 ...

  2. Spring Boot 2 (二):Spring Boot 2 尝鲜-动态 Banner

    Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner,今天我们就先拿这个来尝尝鲜. 配置依赖 使用 Spring Boot 2.0 首先需要将项目依赖包替换为刚刚发 ...

  3. CODE【VS】3160 最长公共子串 (后缀自动机)

    3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Outp ...

  4. C++标准模板库 ——堆栈使用

    include using namespace std; stack S; S.push(i); S.pop(); int x = S.top(); ```

  5. [POJ3463] Sightseeing(次短路 Heap + Dijkstra)

    传送门 用dijkstra比较好,spfa可能有的重复 dis[x][2]:dis[x][0]表示起点到x的最短路.dis[x][1]表示起点到x的次短路: tot[x][2]:tot[x][0]表示 ...

  6. 【树状数组+离线查询】HDU 3333 Turing Tree

    https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/H [题意] 给定一个数组,查询任意区间内不同数字之和. (n<=30000 ...

  7. hdu 2433 Travel (最短路树)

     One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) ...

  8. CodeForces788B 欧拉路

    B. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. PowerDesigner物理模型用法总结

    1.  生成sql脚本 Database→Generate Database 选择要输出的文件路径,即文件存储路径,并根据需要修改文件名,单击确定后便会生成sql脚本. 在Options选项卡里,可以 ...

  10. Eclipse4.5在线安装Aptana插件及配置代码提示教程

    一.Aptana插件官网地址         我在网上试过登陆到aptana官网后点击下载,选择下载eclipse插件版,然后页面给出一串地址:http://download.aptana.com/s ...