@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. Hash Join是Oracle CBO时代经常出现的一种连接方式

    Hash Join是Oracle CBO时代经常出现的一种连接方式,对海量数据处理时经常出现在执行计划里.本篇的上篇(http://space.itpub.net/17203031/viewspace ...

  2. 深入了解Json Web Token之概念篇

    https://www.freebuf.com/articles/web/180874.html http://www.ruanyifeng.com/blog/2018/07/json_web_tok ...

  3. spring test: 配置文件优先级

    application.properties 默认 application-xxx.properties 高 systemEnvironment 高 test/main/resources/ 同名文件 ...

  4. nginx 资源重定向

    背景:有时候我一些资源(.js .css etc.)等不想访问我本地的,我想重定向到其他 URL 解决:直接修改 nginx 的配置文件 conf,添加下面的代码 #avoid processing ...

  5. JavaWeb xss攻击

    出处: http://www.cnblogs.com/TankXiao/archive/2012/03/21/2337194.html XSS 全称(Cross Site Scripting) 跨站脚 ...

  6. spring整合jpa

    有点类似SSH整合,O(∩_∩)O哈哈~ <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...

  7. 关于vue路由嵌套遇到的坑~

    关键在于子路由中的path问题,path之前不要放/ <!DOCTYPE html> <html lang="en"> <head> <m ...

  8. kmspico

    # process | 在这儿找到了原作者的地址 http://blog.nsfocus.net/kmspico/ | 下面是原作者地址 https://forums.mydigitallife.ne ...

  9. (转)使用介质设备安装 AIX 以通过 HMC 安装分区

    使用介质设备安装 AIX 以通过 HMC 安装分区 原文:https://www.ibm.com/support/knowledgecenter/zh/ssw_aix_72/com.ibm.aix.h ...

  10. pat1017. Queueing at Bank (25)

    1017. Queueing at Bank (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Supp ...