首页 > 编程语言 > 详细

线程2--多线程NSThread

时间:2016-08-01 12:00:39      阅读:219      评论:0      收藏:0      [点我收藏+]

NSThread三种方式创建子线程

/**
 * NSThread创建线程方式1
 * 1> 先创建初始化线程
 * 2> start开启线程
 */
-(void)creatNSThread
{
    NSThread  *thread=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"线程A"];
    //为线程设置一个名称
    thread.name=@"线程A";
    //开启线程
    [thread start];
    
    
    NSThread  *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"线程B"];
    //为线程设置一个名称
    thread2.name=@"线程B";
    //开启线程
    [thread2 start];
}


/**
 * NSThread创建线程方式2
 *创建完线程直接(自动)启动
 */

-(void)creatNSThread2
{
    //    NSThread *thread=[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"创建完线程直接(自动)启动"];
    
    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"创建完线程直接(自动)启动"];
}


/**
 * NSThread创建线程方式3
 * 隐式创建线程, 并且直接(自动)启动
 */

-(void)creatNSThread3
{
    //在后台线程中执行===在子线程中执行
    [self performSelectorInBackground:@selector(run:) withObject:@"隐式创建"];
}



-(void)run:(NSString *)str
{
    //获取当前线程
    NSThread *current=[NSThread currentThread];
    //打印输出
    for (int i=0; i<100; i++) {
        NSLog(@"run---%@---%@%i",current,str,i);
    }
}

 

线程2--多线程NSThread

原文:http://www.cnblogs.com/sunjianfei/p/5724961.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!