• 线程NSThread的使用


    //
    //  ZYThreadViewController.h
    //  Thread
    //
    //  Created by yejiong on 15/11/4.
    //  Copyright © 2015年 zzz. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface ZYThreadViewController : UIViewController
    
    @end
    //
    //  ZYThreadViewController.m
    //  Thread
    //
    //  Created by yejiong on 15/11/4.
    //  Copyright © 2015年 zzz. All rights reserved.
    //
    
    #import "ZYThreadViewController.h"
    
    @interface ZYThreadViewController ()
    
    @end
    
    @implementation ZYThreadViewController
    
    - (void)viewDidLoad {
        self.view.backgroundColor = [UIColor whiteColor];
        
        if ([NSThread isMainThread]) {
            NSLog(@"主线程");
        }else {
            NSLog(@"分线程");
        }
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        //1.在分线程中执行 target 的 selector 方法,传入一个参数 argument。
    //    NSThread* thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"123"];
    //
    //    [thread start];
    //    
    //    [thread release];
        
        //2.创建并开启一个分线程。
    //    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"789"];
        
        //3.隐式的创建一个分线程执行 selector 方法。
        [self performSelectorInBackground:@selector(run:) withObject:@"000"];
    }
    
    //以前在分线程中需要我们自己去创建 NSAutoreleasePool 释放在分线程中使用的 autorelease 变量,现在没有这个限制了。
    - (void)run:(id)object {
        NSLog(@"%@", object);
    
        //设置线程休眠多少秒。
    //    [NSThread sleepForTimeInterval:1.0];
        
        //使线程休眠到某个时间。
    //    [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
    
        if ([NSThread isMainThread]) {
            NSLog(@"主线程");
        }else {
            NSLog(@"分线程");
        }
        
        //回到主线程执行 方法调用者 的 selector 方法。
        [self performSelectorOnMainThread:@selector(mainThreadLog:) withObject:@"456" waitUntilDone:NO];
        //wait
        //YES,等待 selector 方法中的内容执行完毕在继续执行分线程中的内容,阻塞当前线程。
        //NO,不等待,两者同时执行,并发执行。
        
        NSLog(@"----------");
        NSLog(@"----------");
        NSLog(@"----------");
        NSLog(@"----------");
        NSLog(@"----------");
        NSLog(@"----------");
        NSLog(@"----------");
        NSLog(@"----------");
    }
    
    - (void)mainThreadLog:(id)objcet {
        NSLog(@"%@", objcet);
        
        if ([NSThread isMainThread]) {
            NSLog(@"主线程");
        }else {
            NSLog(@"分线程");
        }
    }
    
    
    
    @end
  • 相关阅读:
    MFC添加右键菜单
    人生导师——如何学习C++的Windows方向
    删除CListCtrl中具有某一相同数据的所有行
    向某地址写入值并执行
    问题解决——使用CriticalSection后 0xXXXXXXXX处最可能的异常: 0xC0000005: 写入位置 0x00000014 时发生访问冲突
    问题解决——Win7 64 安装 AutoCAD 2010 32位 和 清华天河PC CAD
    问题解决——在结构体中使用set保存结构体数据
    问题解决——基于MSCOMM32.OCX控件的类在客户机不能创建控件
    问题解决——ShowWindow不显示窗口
    问题解决——cout 输出 CString
  • 原文地址:https://www.cnblogs.com/ningmengcao-ios/p/5771204.html
Copyright © 2020-2023  润新知