//
// 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. Lucene学习笔记:一,全文检索的基本原理

    一.总论 根据http://lucene.apache.org/java/docs/index.html定义: Lucene是一个高效的,基于Java的全文检索库. 所以在了解Lucene之前要费一番 ...

  2. 现代程序设计——homework-07

    1.写在前面 不得不很惭愧地说,在看这些博客之前,我对C++的了解仅限于上过一门特别水的关于C++的公选课.一门只有五节课的专业选修课,写过一点点符合C++语法语法规则的类C程序,偶尔在论坛.博客中看 ...

  3. Linux数据流重定向

    一.什么是数据流重导向: 数据流重导向 (redirect) 由字面上的意思来看,好像就是将『数据传导到其他地方去』,没错-数据流重导向就是将某个命令运行后应该要出现在屏幕上的数据, 给他传输到其他的 ...

  4. 在Delphi7中JSON遍历节点不支持使用IN处理方法

    相关资料:http://www.cnblogs.com/del/archive/2009/10/23/1588690.html Delphi2007源代码: procedure TForm1.Butt ...

  5. HDU 5778 abs (枚举)

    abs 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5778 Description Given a number x, ask positive ...

  6. HDU 5724 Chess (sg函数)

    Chess 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5724 Description Alice and Bob are playing a s ...

  7. poj 1466 Girls and Boys(二分图的最大独立集)

    http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total Submis ...

  8. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  9. CentOS服务器配置发送邮件服务

    CentOS服务器配置发送邮件服务 lsb_release -a 查看linux系统版本 在CentOS6以上版本自带mailx版本12.4 rpm -qa | grep mailx 查看系统自带的m ...

  10. matlab eps中文乱码的解决方法

    直接存成eps总是乱码 最优解决方法是matlab print 保存成jpg,之后用adobe  acrobat pro 打开jpg文件另存为eps