线程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:@""];
} //以前在分线程中需要我们自己去创建 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的使用的更多相关文章
- 线程2--多线程NSThread
NSThread三种方式创建子线程 /** * NSThread创建线程方式1 * 1> 先创建初始化线程 * 2> start开启线程 */ -(void)creatNSThread { ...
- 创建线程方式-NSThread
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- iOS开发Swift篇(02) NSThread线程相关简单说明
iOS开发Swift篇(02) NSThread线程相关简单说明 一 说明 1)关于多线程部分的理论知识和OC实现,在之前的博文中已经写明,所以这里不再说明. 2)该文仅仅简单讲解NSThread在s ...
- iOS-多线程--(pthread/NSThread/GCD/NSOperation)--总结
零.线程的注意点(掌握) .不要同时开太多的线程(~3条线程即可,不要超过5条) .线程概念 > 主线程 : UI线程,显示.刷新UI界面,处理UI控件的事件 > 子线程 : 后台线程,异 ...
- 【学习总结】【多线程】 线程 & 进程 & NSThread(多线程的一套API)
一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开 Chrome.Xcode,系统就会分别启动2个进 ...
- iOS---多线程实现方案一 (pthread、NSThread)
在iOS开发中,多线程是我们在开发中经常使用的一门技术.那么本文章将和大家探讨一下针对于多线程的技术实现.本文主要分为如下几个部分: iOS开发中实现多线程的方式 单线程 pthread NSThre ...
- OC 线程操作2 - NSThread
方法1 :直接创建 alloc init - (void)createNSThread111{ /* 参数1: (nonnull id) 目标对象 self 参数2:(nonnull SEL) ...
- IOS NSThread(线程同步)
@interface HMViewController () /** 剩余票数 */ @property (nonatomic, assign) int leftTicketsCount; @prop ...
- IOS 多线程-NSThread 和线程状态
@interface HMViewController () - (IBAction)btnClick; @end @implementation HMViewController - (void)v ...
随机推荐
- [转]天龙八部服务器端Lua脚本系统
一.Lua脚本功能接口 1. LuaInterface.h/.cpp声明和实现LuaInterface. LuaInterface成员如下: //脚本引擎 FoxLuaScriptmLua ; //注 ...
- ubuntu下通过pip安装pyside
首先安装相关库 sudo apt-get install build-essential git cmake libqt4-dev libphonon-dev python2.7-dev libxml ...
- Navicat 远程连接SQL Server 2014 Express 报08001错误
场景:Navicat 远程连接SQL Server 2014 Express 报08001错误,经查验防火墙端口1434,1433已经打开 过程:1. 一开始觉得是连接名称问题,使用IP地址或者主机名 ...
- warning LNK4098: 默认库“LIBCMT”与其他库的使用冲突;请使用 /NODEFAULTLIB:library
最近在编译库文件后,使用它做APP,遇到如下问题: 1>LIBCMT.lib(invarg.obj) : error LNK2005: __pInvalidArgHandler 已经在 LIBC ...
- .NET Reactor 命令行使用
安装.NET Reactor工具软件.例如你的安装目录为:D:\Program Files\Eziriz\.NET Reactor 按如下步骤设置系统环境变量path. 将path变量的值中加入.NE ...
- 【转】Nginx系列(三)--管理进程、多工作进程设计
原博文出于:http://blog.csdn.net/liutengteng130/article/details/46700999 感谢! Nginx由一个master进程和多个worker进程组 ...
- UVALive 7281 Saint John Festival (凸包+O(logn)判断点在凸多边形内)
Saint John Festival 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/J Description Porto's ...
- 在 Web 层应用程序中使用Spring
前面已经配置成功后,就可以在Web 层的Servlet或Jsp中调用访问Spring了,如果你 编制的是一个Servlet/Jsp 程序,那么在你的Servlet/Jsp 使用下面的代码通过Sprin ...
- 转载C# 对象转Json序列化
转载原地址: http://www.cnblogs.com/plokmju/p/ObjectByJson.html JSON Json(JavaScript Object Notation) 是一种 ...
- Protobuf一键生成代码bat文件
最近在摆弄Unity的Socket,需要用到Protobuf,一般都会有多个协议文件,所以研究了下bat的批处理,下面给出批处理文件代码: @echo off ::协议文件路径, 最后不要跟“\”符号 ...