• 多线程 NSThread 了解


    用NSThread创建子线程的3种方法
     
    //  DYFViewController.m
    //  623-02-pthread
    //
    //  Created by dyf on 14-6-23.
    //  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
    //
     
    #import "DYFViewController.h"
    #import <pthread.h>
     
    @interface DYFViewController ()
     
    @end
     
    @implementation DYFViewController
     
    //// c语言函数
    //void *run(void *data)
    //{
    //    // 1.获取当前的线程
    //    NSThread *cThread = [NSThread currentThread];
    //   
    //    // 2.打印线程
    //    NSLog(@"%@", cThread);
    //   
    //    // 3.h耗时操作
    //    for (int i = 0; i < 9999; i++) {
    //        NSLog(@"%@", cThread);
    //    }
    //
    //    return NULL;
    //}
     
    - (IBAction)btnOnClick {
        // 1.获取当前的线程
        NSThread *cthread = [NSThread currentThread];
         
        NSThread *mt = [NSThread mainThread];
        // 2.打印线程
        NSLog(@"%@", cthread);
         
        NSLog(@"%@", mt);
         
        // 3.执行一线耗时的操作 : 创建一套子线程
        [self threadCreate3];
       
    }
    - (void)run:(NSString *)parma
    {
    //    [NSThread threadPriority];
    //   
    //    [NSThread setThreadPriority:0.55];
        // 取值0-1,默认0.5
        for (int i = 0; i < 9999; i++) {
            NSLog(@"%@---------%@", [NSThread currentThread], parma);
        }
    }
     
    - (void)threadCreate5
    {
        // 分离出的子线程
        [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"2222222"];
    }
    - (void)threadCreate4
    {
        // 分离出的子线程
        [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"2222222"];
    }
     
    /**
     *  NSThread创建方式3:隐世线程创建,并且直接(自动)启动
     */
    - (void)threadCreate3
    {
        [self performSelectorInBackground:@selector(run:) withObject:@"333333"];
    }
     
    /**
     *  创建方式2:创建完线程后自动启动
     */
    - (void)threadCreate2
    {
        // 分离出的子线程
        [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"2222222"];
    }
     
    /**
     *  创建方式1:①先创建初始化子线程②再启动
     */
    - (void)threadCreate
    {
        NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"heheh"];
        thread1.name = @"thread1";
        // 开启线程
        [thread1 start];
         
        NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"heheh"];
        thread2.name = @"thread2";
        // 开启线程
        [thread2 start];
         
        NSThread *thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"heheh"];
        thread3.name = @"33";
        // 开启线程
        [thread3 start];
    }
     
    @end

     利用NSThread在开发中也不常用,了解即可

     
     
  • 相关阅读:
    团队项目冲刺第五天
    团队项目冲刺第四天
    团队项目冲刺第三天
    团队项目冲刺第二天
    团队项目冲刺第一天
    团队任务命题
    java报错the superclass was not found 解决方案
    Buildings 分类: ACM 多校 2015-07-23 22:09
    1009 数字1的数量 分类: 51nod 2015-07-20 21:44 3人阅读 评
    1284 2 3 5 7的倍数 分类: 51nod 2015-07-18 22:06 6人阅读
  • 原文地址:https://www.cnblogs.com/Cheetah-yang/p/4664138.html
Copyright © 2020-2023  润新知