@interface HMViewController ()
- (IBAction)btnClick; @end @implementation HMViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (IBAction)btnClick {
// 1.获得当前的线程
NSThread *current = [NSThread currentThread];
NSLog(@"btnClick---%@", current); // NSThread *main = [NSThread mainThread];
// NSLog(@"btnClick---%@", main); // 2.执行一些耗时操作 : 创建一条子线程
[self threadCreate];
} - (void)run:(NSString *)param
{
NSThread *current = [NSThread currentThread]; for (int i = ; i<; i++) {
NSLog(@"%@----run---%@", current, param);
}
} /**
* NSThread的创建方式
* 隐式创建线程, 并且直接(自动)启动
*/
- (void)threadCreate3
{
// 在后台线程中执行 === 在子线程中执行
[self performSelectorInBackground:@selector(run:) withObject:@"abc参数"];
} /**
* NSThread的创建方式
* 创建完线程直接(自动)启动
*/
- (void)threadCreate2
{
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"我是参数"];
} /**
* NSThread的创建方式
* 1> 先创建初始化线程
* 2> start开启线程
*/
- (void)threadCreate
{
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"哈哈"];
thread.name = @"线程A";
// 开启线程
[thread start]; NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"哈哈"];
thread2.name = @"线程B";
// 开启线程
[thread2 start];
}
@end

线程的状态

- (void)viewDidLoad
{
[super viewDidLoad]; self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(test) object:nil];
self.thread.name = @"线程A";
} - (void)test
{
NSLog(@"test - 开始 - %@", [NSThread currentThread].name); // [NSThread sleepForTimeInterval:5]; // 阻塞状态 // NSDate *date = [NSDate dateWithTimeIntervalSinceNow:5.0];
// [NSThread sleepUntilDate:date]; for (int i = ; i<; i++) {
NSLog(@"test - %d - %@", i, [NSThread currentThread].name); if (i == ) {
[NSThread exit];
}
} NSLog(@"test - 结束 - %@", [NSThread currentThread].name);
}

IOS 多线程-NSThread 和线程状态的更多相关文章

  1. iOS多线程 NSThread/GCD/NSOperationQueue

    无论是GCD,NSOperationQueue或是NSThread, 都没有线程安全 在需要同步的时候需要使用NSLock或者它的子类进行加锁同步 "] UTF8String], DISPA ...

  2. iOS多线程全套:线程生命周期,多线程的四种解决方案,线程安全问题,GCD的使用,NSOperation的使用

    目的 本文主要是分享iOS多线程的相关内容,为了更系统的讲解,将分为以下7个方面来展开描述. 多线程的基本概念 线程的状态与生命周期 多线程的四种解决方案:pthread,NSThread,GCD,N ...

  3. IOS 多线程 NSThread

    一个正在运行的应用程序是一个进程,一个进程会默认开启一个主线程,但是在主线程中的操作是串行的,也就是当有多个任务同时需要完成的时候,是按照顺序一个个执行.因此,为了提高效率,会在进程中开启多个线程,每 ...

  4. ios多线程NSThread

    1.简介: 1.1 iOS有三种多线程编程的技术,分别是: 1..NSThread 2.Cocoa NSOperation (iOS多线程编程之NSOperation和NSOperationQueue ...

  5. iOS多线程编程:线程同步总结

    1:原子操作 - OSAtomic系列函数 iOS平台下的原子操作函数都以OSAtomic开头,使用时需要包含头文件<libkern/OSBase.h>.不同线程如果通过原子操作函数对同一 ...

  6. Java 多线程创建和线程状态

    一.进程和线程 多任务操作系统中,每个运行的任务是操作系统运行的独立程序. 为什么引进进程的概念? 为了使得程序能并发执行,并对并发执行的程序加以描述和控制. 因为通常的程序不能并发执行,为使程序(含 ...

  7. 【多线程】观测线程状态 getState()

    观测线程状态 getState() Thread.State(查看JDK帮助文档) 线程状态.线程可以处于以下状态之一: [NEW] 尚未启动的线程处于此状态. [RUNNABLE] 在Java虚拟机 ...

  8. iOS多线程编程之线程的状态(转载)

    一.简单介绍 线程的创建: self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil]; ...

  9. iOS多线程NSThread和GCD

    在iOS中啊  其实有多种方法实现多线程 这里只记录两个比较常用的  或者说我比较常用的 一个就是BSThread 另一个就是一听名字就比较霸气的妇孺皆知的GCD 先说一下NSThread吧 这个方式 ...

随机推荐

  1. 51Nod - 1242 斐波那契(快速幂)

    斐波那契数列的定义如下:   F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2)   (1, 1, 2, 3, 5, 8, 13, 21, ...

  2. shell编程中

    1.1 条件表达式 1.1.1 文件判断 常用文件测试操作符 常用文件测试操作符 说明 -d文件,d的全拼为directory 文件存在且为目录则为真,即测试表达式成立 -f文件,f的全拼为file ...

  3. rhcs红帽插件及 轮循

    server1:yum install luci ricci -yecho westos | passwd -stdin  ricci/etc/init.d/ricci startchkconfig ...

  4. 如何使用线程安全的HashMap

    转载:https://blog.csdn.net/qq_31493821/article/details/78855069 HashMap为什么线程不安全 导致HashMap线程不安全的原因可能有两种 ...

  5. docker+ bind mount 部署复杂flask应用

    报错如下: [root@test-wenqiang flask-skeleton]# docker run -d -p 80:5000 -v $(pwd):/skeleton --name flask ...

  6. prototype.js

    (1)$() 方法是在DOM中使用过于频繁的 document.getElementById() 方法的一个便利的简写, 就像这个DOM方法一样,这个方法返回参数传入的id的那个元素. (2)

  7. java替换word2003

    map.put("year", year);            map.put("yearMonthDay", yearMonthDay);         ...

  8. Golang常用数据结构(对照python)

    python golang init get set extend/update find index size loop list list l := list.New()   l.PushBack ...

  9. 《深入浅出Node.js》学习笔记(一)

    看了朴灵前辈的node.js系列文章,很开阔视野,虽然能力有限还是有很多不懂,但是还是希望能写下笔记,初步对node.js有点了解. 一.概念 Node.js不是JS应用.而是JS运行平台 Node. ...

  10. PhpStorm快捷键设置,个性化设置,多项目共存,更改样式主题字体

    自定义常用快捷键 按照路径:File -> Settings -> Appearance & Behavior -> Keymap -> Copy 一份 Eclipse ...