博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第一篇:NSTread线程的创建
阅读量:6900 次
发布时间:2019-06-27

本文共 1925 字,大约阅读时间需要 6 分钟。

 

#import "ViewController.h"

//导入头文件

#import <pthread.h>

@interfaceViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

    [superviewDidLoad];

    [selfbtnClick];

}

/*

 *NSThread创建线程方式

 *1.先初始化线程

 *2.start开启线程

 */

-(void)btnClick

{

 

    //1.获取当前线程

    NSThread * current = [NSThreadcurrentThread];

    

    //主线程

    NSLog(@"进入主线程--%@",current);

    

    //获取主线程的另一中方式

    NSThread*main =[NSThreadmainThread];

    NSLog(@"主线程2--%@",main);

    

    //执行一些耗时操作(分多一个线程)

    [selfcreatNSTread];

    

}

-(void)creatNSTread

{

    NSThread * thread1=[[NSThreadalloc]initWithTarget:selfselector:@selector(run:) object:@"线程A"];

    //设置一个名称

    thread1.name=@"线程A";

    //开启线程

    [thread1 start];

    

    

    

    NSThread * thread2=[[NSThreadalloc]initWithTarget:selfselector:@selector(run:) object:@"线程B"];

    //设置一个名称

    thread2.name=@"线程B";

    //开启线程

    [thread2 start];

 

 

 

 

}

 

-(void)run:(NSString*)str

{

//获取当前线程

    NSThread * current=[NSThreadcurrentThread];

    

    //打印输出

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

        NSLog(@"run--%@--%@",current,str);

    }

    

 

}

运行结果:

 

 

 

 

/******************************创建NSTread线程方式2**************************************/

 

 

 

#import "ViewController.h"

//导入头文件

#import <pthread.h>

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    [self btnClick];

}

 

-(void)btnClick

{

 

    //1.获取当前线程

    NSThread * current = [NSThread currentThread];

    

    //主线程

    NSLog(@"进入主线程--%@",current);

    

    //获取主线程的另一中方式

    NSThread*main =[NSThread mainThread];

    NSLog(@"主线程2--%@",main);

    

    //执行一些耗时操作(分多一个线程)

    [self creatNSTread2];

    [self creatNSTead3];

    

}

/*

 *NSTread创建线程方式2

 *创建完线程直接启动(自动)

 */

-(void)creatNSTread2

{

    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"第一线程"];

 

 

}

-(void)creatNSTead3

{

 //在后台线程执行在子线程中执行

    [self performSelectorInBackground:@selector(run:) withObject:@"第二线程"];

 

 

}

-(void)run:(NSString*)str

{

//获取当前线程

    NSThread * current=[NSThread currentThread];

    

    //打印输出

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

        NSLog(@"run--%@--%@",current,str);

    }

    

}

 

运行结果:

转载于:https://www.cnblogs.com/jinchengvs/p/4835994.html

你可能感兴趣的文章
用telnet命令,POP3接收邮件
查看>>
Nginx 关于 location 的匹配规则详解
查看>>
OutputStream、InputStream 、FileOutputStream、FileInputStream,字节流API
查看>>
10. Python面向对象
查看>>
python3与 python2 urllib模块区别
查看>>
关于props 和state
查看>>
跟我学算法-tensorflow 实现线性拟合
查看>>
redis使用管道pipeline提升批量操作性能(php演示)
查看>>
python: file_handling 解决工作中出现的文件处理需求
查看>>
HTML5 拖放(Drag 和 Drop)功能开发——浅谈dataTransfer对象
查看>>
灰度图像亮度对比度调整的简单代码
查看>>
shell测试题上机实验
查看>>
[转]二维数组和二级指针的传递问题
查看>>
nginx+fastcgi+c/c++搭建高性能Web框架
查看>>
[转载]安装archlinux 以后没有 ifconfig,route ,nslo
查看>>
人见人爱A^B
查看>>
zoj 3795 Grouping tarjan缩点 + DGA上的最长路
查看>>
浏览器内核
查看>>
zabbix-server安装部署配置
查看>>
终于解决 xUnit.net 测试中无法输出到控制台的问题
查看>>