IOS开发网络第一天之02NSThread的基本使用

Stella981
• 阅读 620

创建子线程的三种方法

1.第一种方式,创建线程后必须要开启线程!

 NSThread *thread = [[NSThread alloc]initWithTarget:

                        self selector:@selector(longTimeOperation) object:nil];

开启线程

    [thread start];

2.这种方式创建就开启

[self performSelectorInBackground:@selector(longTimeOperation)

                           withObject:nil];

3。这种方式创建就开启

[NSThread detachNewThreadSelector:@selector(longTimeOperation)

                             toTarget:self withObject:nil];

#pragma mark - UITouch操作

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

//耗时操作在主线程中执行;

// [self longTimeOperation];

    [self test4];

}

#pragma mark - NSThread//

//NSThread相关方法

- (void) test5{

//1.获取当前线程

    NSThread *thread1 = [NSThread currentThread];

//2.获取主线程

    NSThread *mainThread  = [NSThread mainThread];

    NSLog(@"%@",thread1);

    NSLog(@"%@",mainThread);

}

#pragma mark - NSTread的使用

//NSThread相关的线程属性

- (void)test4{

//1。创建一个线程对象

    NSThread *thread = [[NSThread alloc]initWithTarget:

                        self selector:@selector(longTimeOperation) object:nil];

//2.开启线程

    [thread start];

//===================线程属性=================

//1.线程名

//对于功能的实现没有任何实际意义

//主要是方便调试

    [thread setName:@"下载线程"];

//2.优先级(0-1,值越大,优先级越高)

//优先级不是指哪个优先级高就先被执行,而是线程被调度的次数多(

//cpu调度到得子线程优先级高,执行的时间就越长);

//时间开发中不会去设置优先级;

//如果只有一个子线程,优先级多少都没有任何影响

// [thread setThreadPriority:1];

    //3.

    NSThread *thread2 = [[NSThread alloc]initWithTarget:

                         self selector:@selector(longTimeOperation) object:nil];

    [thread2 start];

    thread2.name = @"解析线程";

}

//隐式创建一个子线程

- (void)test3{

[self performSelectorInBackground:@selector(longTimeOperation)

                           withObject:nil];

}

//快速开启一个子线程

- (void)test2{

//参数1:需要在子线程中执行的方法

//参数2:调用方法的对象

//参数3:方法参数的实参

    [NSThread detachNewThreadSelector:@selector(longTimeOperation)

                             toTarget:self withObject:nil];

}

//创建NSTread对象

- (void) test1{

//1.创建一个NSThread对象

//一个NSThread对象就是一个子线程

//参数1:响应消息的对象;

//参数2:消息(需要在子线程中执行的方法)

//参数3:参数2的方法的参数的实参

    NSThread *thread = [[NSThread alloc]initWithTarget:

                        self selector:@selector(longTimeOperation) object:nil];

//2.开启线程

    [thread start];

}

#pragma mark - 耗时操作

- (void) longTimeOperation{

    for (int i = 0; i<10000; i++) {

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

    }

}

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
3个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
JS 苹果手机日期显示NaN问题
问题描述newDate("2019122910:30:00")在IOS下显示为NaN原因分析带的日期IOS下存在兼容问题解决方法字符串替换letdateStr"2019122910:30:00";datedateStr.repl
Wesley13 Wesley13
3年前
MySQL总结(十一)子查询
!(https://oscimg.oschina.net/oscnet/upa344f41e81d3568e3310b5da00c57ced8ea.png)子查询1\.什么是子查询需求:查询开发部中有哪些员工selectfromemp;通
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
9个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这