第01天多线程网络:(07):NSThread创建线程的生命周期
2017-04-15 00:00
309 查看
#####一、NSThread创建线程的生命周期
code
LYHThread(写一个
ViewController
线程的生命周期: 当任务执行完毕之后被释放掉
code
LYHThread(写一个
继承NSThread的类,调用
dealloc方法就
能查看什么时候被销毁的了)
#import <Foundation/Foundation.h> @interface LYHThread : NSThread @end ---- #import "LYHThread.h" @implementation LYHThread - (void)dealloc { NSLog(@"%s -- %@",__func__,[NSThread currentThread]); } @end
ViewController
/* 线程的生命周期: 当任务执行完毕之后被释放掉 */ #import "ViewController.h" #import "LYHThread.h" #import <pthread.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { #pragma 1.NSThread [self createNewThread1]; } #pragma 方式1 通过 - (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(id)argument; 创建子线程是属于一个休眠的状态的 - (void)createNewThread1 { //1.创建线程,并且执行那个方法 LYHThread *threadA = [[LYHThread alloc]initWithTarget:self selector:@selector(run:) object:@"abc"]; // 设置属性 threadA.name = @"线程A"; // 线程名称 // 设置优先级 取值范围0.0 ~ 1.0 之间 最高是1.0 默认优先级是0.5 threadA.threadPriority = 1.0; //2.执行线程 [threadA start]; } - (void)run:(NSString *)parma { for (int i = 0; i<10000; i++) { NSLog(@"%zd --- %@",i,[NSThread currentThread]); } } void * _Nullable task(void * param) { // 把耗时操作放到子线程 for (int i = 0; i<10000; i++) { NSLog(@"%zd --- %@",i,[NSThread currentThread]); } // 返回一个null NSLog(@"__ %@",[NSThread currentThread]); // <NSThread: 0x61000006b900>{number = 3, name = (null)} // number 不等于1 就是子线程 return NULL; } @end
相关文章推荐
- 猫猫学iOS(四十九)多线程网络之线程的创建NSThreand
- 第01天多线程网络:(06):NSThread的基本使用
- 网络开始---多线程---NSThread-02-线程状态(了解)(三)
- AJ学IOS(49)多线程网络之线程的创建NSThreand
- 第01天多线程网络:(16):GCD实现线程间通信
- 【搞懂Java多线程之一】多线程相关概念,线程生命周期以及线程创建方法
- 第01天多线程网络:(08):线程的状态(了解)
- 【Java多线程】什么是线程与线程的生命周期与线程的创建方式
- Java —— 多线程笔记 一、线程创建、启动、生命周期、线程控制
- 第01天多线程网络:(11):NSThread实现线程间通信
- 第01天多线程网络:(01):进程和线程
- 网络多线程-NSThread实现线程间通信
- 多线程学习-线程基本概念及线程创建--发布日期:2008-07-16 23:19
- 多线程创建 方法一: NSThread 创建线程的三种方式
- linux多线程编程1--线程的创建
- C# 多线程编程 - 使用Thread类创建线程
- 利用委托解决多线程中“不是此控件创建的线程”的问题
- 多线程专题之MFC创建线程(1)
- asp.net 创建和终止线程(多线程)
- Java6学习笔记55——多线程编程——线程的创建方法1