线程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 ...
随机推荐
- mahout协同过滤算法
一直使用mahout的RowSimilarity来计算物品间的相似度,今晚仔细看了其实现,终于搞明白了他的计算逻辑. 上篇中介绍了整个itemBaseCF的mapreducer过程,主要有三个大的步骤 ...
- Hadoop上路-02_Hadoop FS Shell
一.上传文件/目录 1)put 从本地文件系统中复制N个源路径到目标文件系统. 2)copyFromLocal 源路径须是一个本地文件. 二.下载文件/目录 1)get 复制文件到本地文件系统. 2) ...
- flot图表的使用
Flot是一套用Javascript写的绘制图表用的函式库, 专门用在网页上执行绘制图表功能, 由于Flot利用jQuery所以写出来的, 所以也称它为jQuery Flot ,它的特点是体积小.执行 ...
- Windows Server 2012 四个版本对比
Windows Server 2012 有4种版本: Foundation, Essentials, Standard and Datacenter. 版本 Foundation Essentials ...
- openstack 网络
物理节点hosts解析配置
- HDU 4901 The Romantic Hero (计数DP)
The Romantic Hero 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/E Description There is ...
- HDU 3072 Intelligence System (强连通分量)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 自定义控件和XControl控件
(1)LabVIEW的自定义控件,实际上就是对LabVIEW自带的控件的一种修改,但是这种修改只能改变它的外观,即大小.颜色.位置等等,但是功能是改变不了的.如你对一个按钮进行自定义控件,无论怎么改, ...
- IMAQ Flatten Image to String VI的参数设置对比
无压缩 jpeg压缩 无损二元包装 仅JPEG压缩时有效 平化类型(指定字符串中存储什么类型的数据) None JPEG PACKED BINARY Quality Image Image and ...
- 12个有趣的C语言面试题
摘要:12个C语言面试题,涉及指针.进程.运算.结构体.函数.内存,看看你能做出几个! 1.gets()函数 问:请找出下面代码里的问题: #include<stdio.h> int ma ...