@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. RequestContextHolder与RequestContextUtils

    org.springframework.web.servlet.support.RequestContextUtils 在spring-webmvc中, 主要用来获取WebApplicationCon ...

  2. kmspico

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

  3. Unity 双击Esc或者返回退出游戏,有文字提示

    第一次点击Esc或者返回,显示提示文字"再次按下返回键退出游戏",在文字消失之前再次点击Esc或者返回,退出游戏. 此脚本挂在Text文字提示上: using UnityEngin ...

  4. 网络编程api总结

    1.socket函数创建一个socket连接,此时该socket连接为主动式. fd(int)->fd(struct fd)->file->sock:sock->file 2. ...

  5. IO流等学习笔记

    1.为什么日期的开始是从1970年0101开始记录,计算机的日期记录是现在的时间距1970年的时间,可正可负.? 2.引用类型默认都为null,基本数据类型为0,除基本数据类型外所有的都为引用数据类型 ...

  6. windows下端口转发 netsh

    添加映射表: netsh interface portproxy add v4tov4 listenport=(监听端口) connectaddress=(虚机IP) connectport=(虚机端 ...

  7. redis开机启动,有密码

    #!/bin/sh # chkconfig: # description: Start and Stop redis REDISPORT= EXEC=/usr/local/redis/src/redi ...

  8. java多线程优先级问题

    java 中的线程优先级的范围是1-10,默认的优先级是5.“高优先级线程”会优先于“低优先级线程”执行. 例子: package com.ming.thread.threadpriority; pu ...

  9. 关于get_magic_quotes_gpc()函数(交互数据转义的判断)

    在PHP中get_magic_quotes_gpc()函数是内置的函数,这个函数的作用就是得到php.ini设置中magic_quotes_gpc选项的值. 那么就先说一下magic_quotes_g ...

  10. [转]在离线环境中发布.NET Core至Windows Server 2008

    本文转自:http://www.cnblogs.com/durow/p/5765145.html 0x00 写在开始 之前一篇博客中写了在离线环境中使用.NET Core,之后一边学习一边写了一些页面 ...