zoukankan      html  css  js  c++  java
  • ios31--NSThread

    //
    //  ViewController.m
    //  03-掌握-NSThread基本使用
    
    #import "ViewController.h"
    #import "XMGThread.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        [self createNewThread1];
    }
    
    //1.alloc init 创建线程,需要手动启动线程,根据线程的number判断是不是同一个线程。
    //线程的生命周期:当任务执行完毕之后被释放掉(比较特殊,)
    -(void)createNewThread1
    {
        //1.创建线程
        /*  第一个参数:目标对象  self
         第二个参数:方法选择器 调用的方法
         第三个参数:前面调用方法需要传递的参数 nil  */
        
        //XMGThread是继承自NSThread的类,threadA是局部变量,大括号之后就销毁了,
        XMGThread *threadA = [[XMGThread alloc]initWithTarget:self selector:@selector(run:) object:@"ABC"];
        //设置属性
        threadA.name = @"线程A";//属性用点语法和set方法是一样的。
        //设置优先级  取值范围 0.0 ~ 1.0 之间 最高是1.0 默认优先级是0.5,获取cpu的几率更大,
        threadA.threadPriority = 1.0;
        //2.启动线程,// 进入就绪状态 -> 运行状态。当线程任务执行完毕,自动进入死亡状态
        [threadA start];
    
        
        NSThread *threadB = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"ABC"];//object是run方法的参数,处于暂停状态。
        threadB.name = @"线程b";
        threadB.threadPriority = 0.1;
        [threadB start];
        
        
        NSThread *threadC = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"ABC"];
        threadC.name = @"线程C";
        [threadC start];
        
        [self createNewThread3];
        
    }
    
    
    //2.分离子线程,自动启动线程,不需要start,拿不到线程对象,无法对线程进行更详细的设置
    -(void)createNewThread2
    {
        [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"分离子线程"];
    }
    
    
    //3.开启一条后台线程,不需要start,拿不到线程对象,无法对线程进行更详细的设置
    -(void)createNewThread3
    {
        [self performSelectorInBackground:@selector(run:) withObject:@"开启后台线程"];
    }
    
    
    -(void)run:(NSString *)param
    {
         NSLog(@"---run----%@---%@",[NSThread currentThread].name,param);
        for (NSInteger i = 0; i<1; i++) {
            NSLog(@"%zd----%@",i,[NSThread currentThread].name);
        }
    }
    
    @end
    
    
    /*
     启动线程
     - (void)start;
     // 进入就绪状态 -> 运行状态。当线程任务执行完毕,自动进入死亡状态
     
     阻塞(暂停)线程
     + (void)sleepUntilDate:(NSDate *)date;
     + (void)sleepForTimeInterval:(NSTimeInterval)ti;
     // 进入阻塞状态
     
     强制停止线程
     + (void)exit;
     // 进入死亡状态
     
     注意:一旦线程停止(死亡)了,就不能再次开启任务
     */
    //
    //  XMGThread.h
    //  03-掌握-NSThread基本使用
    //
    //  Created by xiaomage on 16/2/18.
    //  Copyright © 2016年 小码哥. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface XMGThread : NSThread
    
    @end
    //
    //  XMGThread.m
    //  03-掌握-NSThread基本使用
    //
    //  Created by xiaomage on 16/2/18.
    //  Copyright © 2016年 小码哥. All rights reserved.
    //
    
    #import "XMGThread.h"
    
    @implementation XMGThread
    
    -(void)dealloc
    {
        NSLog(@"dealloc----%@",[NSThread currentThread]);  //线程的run方法执行完之后,才调用。
    }
    @end
  • 相关阅读:
    HDU-6315 Naive Operations 线段树
    18牛客第二场 J farm
    POJ
    SPOJ
    codeforces 501C. Misha and Forest
    Codeforces 584C
    Domination
    HDU-3074 Multiply game
    Codefoeces-689D Friends and Subsequences
    Codeforces Round #486 (Div. 3)
  • 原文地址:https://www.cnblogs.com/yaowen/p/7489412.html
Copyright © 2011-2022 走看看