iOS进阶_多线程(二.线程间的状态)

这里写图片描述

//
//  ViewController.m
//  004-NSThread状态
//
//  Created by mac on 2018/4/27.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self threadDemo];
//杀主线程
NSLog(@"走!!");
//注意:exit 方法没法杀掉主线程!但是会让主线程阻塞!!
//    [NSThread exit];
//    [[NSThread currentThread] start];
//    exitDemo
//    [self exitDemo];

}

-(void)exitDemo{
    [self performSelectorInBackground:@selector(starMainThread) withObject:nil];
    //注意!!!exit会让杀掉主线程!但是APP不会挂掉
    [NSThread exit];
}

-(void)starMainThread{
    [NSThread sleepForTimeInterval:1.0];
    //开启主线程
    [[NSThread mainThread] start];
}

-(void)threadDemo{
    //创建线程
    NSThread * t =[[NSThread alloc]initWithTarget:self selector:@selector(threadStatus) object:nil];
    //线程就绪(CPU翻牌)
    [t start];
}
-(void)threadStatus{
    for (int i = 0; i<20; i++) {
        //阻塞,当运行满足某个条件,会让线程“睡一会”
        //提示:sleep 方法是类方法,会直接休眠当前线程!!
        if(i == 8){
            NSLog(@"睡一会,2秒");
            [NSThread sleepForTimeInterval:2.0];
        }
        NSLog(@"%@  %d",[NSThread currentThread],i);

        //当线程满足某一个条件时,可以强行终止的
        //exit 类方法,终止当前线程!!
        if(i ==15){
            //一旦强行终止线程,后续的所有代码都不会被执行
            //注意:在终止线程之前,应该要释放之前分配的对象
            [NSThread exit];
        }
    }
    NSLog(@"能来吗??答案:来不了");
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值