//
// 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:@""];
} //以前在分线程中需要我们自己去创建 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:@"" 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

线程NSThread的使用的更多相关文章

  1. 线程2--多线程NSThread

    NSThread三种方式创建子线程 /** * NSThread创建线程方式1 * 1> 先创建初始化线程 * 2> start开启线程 */ -(void)creatNSThread { ...

  2. 创建线程方式-NSThread

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  3. iOS开发Swift篇(02) NSThread线程相关简单说明

    iOS开发Swift篇(02) NSThread线程相关简单说明 一 说明 1)关于多线程部分的理论知识和OC实现,在之前的博文中已经写明,所以这里不再说明. 2)该文仅仅简单讲解NSThread在s ...

  4. iOS-多线程--(pthread/NSThread/GCD/NSOperation)--总结

    零.线程的注意点(掌握) .不要同时开太多的线程(~3条线程即可,不要超过5条) .线程概念 > 主线程 : UI线程,显示.刷新UI界面,处理UI控件的事件 > 子线程 : 后台线程,异 ...

  5. 【学习总结】【多线程】 线程 & 进程 & NSThread(多线程的一套API)

    一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开 Chrome.Xcode,系统就会分别启动2个进 ...

  6. iOS---多线程实现方案一 (pthread、NSThread)

    在iOS开发中,多线程是我们在开发中经常使用的一门技术.那么本文章将和大家探讨一下针对于多线程的技术实现.本文主要分为如下几个部分: iOS开发中实现多线程的方式 单线程 pthread NSThre ...

  7. OC 线程操作2 - NSThread

        方法1 :直接创建 alloc init - (void)createNSThread111{ /* 参数1: (nonnull id) 目标对象 self 参数2:(nonnull SEL) ...

  8. IOS NSThread(线程同步)

    @interface HMViewController () /** 剩余票数 */ @property (nonatomic, assign) int leftTicketsCount; @prop ...

  9. IOS 多线程-NSThread 和线程状态

    @interface HMViewController () - (IBAction)btnClick; @end @implementation HMViewController - (void)v ...

随机推荐

  1. 【Spark学习】Apache Spark for 第三方Hadoop分发版

    Spark版本:1.1.1 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4137979.html

  2. .NET Reactor 命令行使用

    安装.NET Reactor工具软件.例如你的安装目录为:D:\Program Files\Eziriz\.NET Reactor 按如下步骤设置系统环境变量path. 将path变量的值中加入.NE ...

  3. 第二百五十天 how can I 坚持

    html排版,好烦心. 我以为我会哭,但是我没有.---<领悟> 确实是搞不懂自己,到底想要什么?弟弟最近也不知道咋的了,感觉有点不对劲呢. 最近雾霾好严重,希望我们的后代不会知道雾霾是什 ...

  4. Can jxta be used to develop online card game (p2p style)?

    Can jxta be used to develop online card game (p2p style)? https://www.java.net//node/677134 I am new ...

  5. ubuntu 备份安装程序列表

    一般情况下,我们重装ubuntu的系统会做如下几个事情 1)修改默认的程序更新源 2)开始根据需求安装软件. 3)配置文件(如vim/tmux等) 对于步骤,只需要cp /etc//etc/apt/s ...

  6. Oracle 查看表空间大小及其扩展

    在ORACLE数据库中,所有数据从逻辑结构上看都是存放在表空间当中,当然表空间下还有段.区.块等逻辑结构.从物理结构上看是放在数据文件中.一个表空间可由多个数据文件组成.系统中默认创建的几个表空间:S ...

  7. poj 1469 COURSES(匈牙利算法模板)

    http://poj.org/problem?id=1469 COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

  8. Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (类似LCA)

    题目链接:http://codeforces.com/problemset/problem/697/D 给你一个有规则的二叉树,大概有1e18个点. 有两种操作:1操作是将u到v上的路径加上w,2操作 ...

  9. Visifire的一些使用心得

    1.如何让图表的Y轴不从0开始显示:有时一系列的数据差别很小,如果从0开始显示,在Y轴上,会一堆数据都堆在某一个区间.例如期货的蜡烛图.将ViewportRangeEnabled设为true即可解决此 ...

  10. 通过SCVMM分配SMB 3.0 文件共享

    1.创建SMB群集共享,赋予Hyper-V主机. Hyper-V群集名称.Hyper-V管理员.Hyper-V服务账户完全控制权限 2.VMM提供程序导入 文件服务器(运行方式账户要对文件服务器群集的 ...