多线程 NSThread 了解
// DYFViewController.m
// 623-02-pthread
//
// Created by dyf on 14-6-23.
// Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//
#import "DYFViewController.h"
#import <pthread.h>
@interface
DYFViewController ()
@end
@implementation
DYFViewController
//// c语言函数
//void *run(void *data)
//{
// // 1.获取当前的线程
// NSThread *cThread = [NSThread currentThread];
//
// // 2.打印线程
// NSLog(@"%@", cThread);
//
// // 3.h耗时操作
// for (int i = 0; i < 9999; i++) {
// NSLog(@"%@", cThread);
// }
//
// return NULL;
//}
- (
IBAction
)btnOnClick {
// 1.获取当前的线程
NSThread
*cthread = [
NSThread
currentThread];
NSThread
*mt = [
NSThread
mainThread];
// 2.打印线程
NSLog
(@
"%@"
, cthread);
NSLog
(@
"%@"
, mt);
// 3.执行一线耗时的操作 : 创建一套子线程
[
self
threadCreate3];
}
- (
void
)run:(
NSString
*)parma
{
// [NSThread threadPriority];
//
// [NSThread setThreadPriority:0.55];
// 取值0-1,默认0.5
for
(
int
i = 0; i < 9999; i++) {
NSLog
(@
"%@---------%@"
, [
NSThread
currentThread], parma);
}
}
- (
void
)threadCreate5
{
// 分离出的子线程
[
NSThread
detachNewThreadSelector:
@selector
(run:) toTarget:
self
withObject:@
"2222222"
];
}
- (
void
)threadCreate4
{
// 分离出的子线程
[
NSThread
detachNewThreadSelector:
@selector
(run:) toTarget:
self
withObject:@
"2222222"
];
}
/**
* NSThread创建方式3:隐世线程创建,并且直接(自动)启动
*/
- (
void
)threadCreate3
{
[
self
performSelectorInBackground:
@selector
(run:) withObject:@
"333333"
];
}
/**
* 创建方式2:创建完线程后自动启动
*/
- (
void
)threadCreate2
{
// 分离出的子线程
[
NSThread
detachNewThreadSelector:
@selector
(run:) toTarget:
self
withObject:@
"2222222"
];
}
/**
* 创建方式1:①先创建初始化子线程②再启动
*/
- (
void
)threadCreate
{
NSThread
*thread1 = [[
NSThread
alloc] initWithTarget:
self
selector:
@selector
(run:) object:@
"heheh"
];
thread1.name = @
"thread1"
;
// 开启线程
[thread1 start];
NSThread
*thread2 = [[
NSThread
alloc] initWithTarget:
self
selector:
@selector
(run:) object:@
"heheh"
];
thread2.name = @
"thread2"
;
// 开启线程
[thread2 start];
NSThread
*thread3 = [[
NSThread
alloc] initWithTarget:
self
selector:
@selector
(run:) object:@
"heheh"
];
thread3.name = @
"33"
;
// 开启线程
[thread3 start];
}
@end
利用NSThread在开发中也不常用,了解即可
多线程 NSThread 了解的更多相关文章
- iOS多线程 NSThread/GCD/NSOperationQueue
无论是GCD,NSOperationQueue或是NSThread, 都没有线程安全 在需要同步的时候需要使用NSLock或者它的子类进行加锁同步 "] UTF8String], DISPA ...
- 多线程NSThread基本用法
#import "ViewController.h" @interface ViewController () @end @implementation ViewContr ...
- [iOS]深入浅出 iOS 之多线程 NSThread
OS 支持多个层次的多线程 编程,层次越高的抽象程度越高,使用起来也越方便,也是苹果最推荐使用的方法. 下面简要说明这三种不同范式: Thread 是这三种范式里面相对轻量级的,但也是使用起 ...
- 多线程 -- NSThread
NSThread NSThread 一个NSThread对象就代表一条线程 创建线程的几种方式 alloc/init // 1.创建线程 NJThread *thread = [[NJThread a ...
- [iOS 多线程 & 网络 - 1.1] - 多线程NSThread
A.NSThread的基本使用 1.创建和启动线程 一个NSThread对象就代表一条线程创建.启动线程NSThread *thread = [[NSThread alloc] initWithTar ...
- 多线程 NSThread GCD
ios多线程实现种类 NSThread NSOperationQueue NSObject GCD *************** 1.NSThread //线程 第一种 NSThread *thre ...
- iOS多线程NSThread和GCD
在iOS中啊 其实有多种方法实现多线程 这里只记录两个比较常用的 或者说我比较常用的 一个就是BSThread 另一个就是一听名字就比较霸气的妇孺皆知的GCD 先说一下NSThread吧 这个方式 ...
- iOS 多线程NSThread理解与场景示例
NSThread是相对GCD和NSOperationQuene而言,比较轻量级的一种多线程处理方式. 但同时,它的弊端就是需要自己管理线程的生命周期,以及线程同步:而另外两种不需要自己管理. 常见方法 ...
- 九、使用多线程——NSThread,GCD和NSOperation
概述 早上起床,你先打开洗衣机,然后用热水把泡面泡上,接着打开电脑开启一天的码农生活.其中“洗衣服”.“泡泡面”和“码代码”3个任务(线程)同时进行,这就是多线程.网上有许多关于多线程的经典解释,此处 ...
- IOS 多线程 NSThread
一个正在运行的应用程序是一个进程,一个进程会默认开启一个主线程,但是在主线程中的操作是串行的,也就是当有多个任务同时需要完成的时候,是按照顺序一个个执行.因此,为了提高效率,会在进程中开启多个线程,每 ...
随机推荐
- tomcat部署web应用的4种方法以及部署多个应用
原文: tomcat部署web应用的4种方法 在Tomcat中有四种部署Web应用的方式,简要的概括分别是: (1)利用Tomcat自动部署 (2)利用控制台进行部署 (3)增加自定义的Web部署文件 ...
- Hadoop实战-使用Eclipse开发Hadoop API程序(四)
一.准备运行所需Jar包 1)avro-1.7.4.jar 2)commons-cli-1.2.jar 3)commons-codec-1.4.jar 4)commons-collections-3. ...
- guava cache与spring集成
缓存的背景 缓存,在我们日常开发中是必不可少的一种解决性能问题的方法.简单的说,cache 就是为了提升系统性能而开辟的一块内存空间.在cpu进行计算的时候, 首先是读取寄存器,然后内存,再是硬盘.由 ...
- HTTP1.1学习笔记 -- RFC2616
本人跟web无缘,从来没有想去学http,现在看来,学学也是有益无害,总会要用着点滴. RFC见这里: https://www.ietf.org/rfc/rfc2616.txt 0. URI格式 ht ...
- 前端面试常考知识点---CSS
前端面试常考知识点---js 1.CSS3的新特性有哪些 点我查看 CSS3选择器 . CSS3边框与圆角 CSS3圆角border-radius:属性值由两个参数值构成: value1 / valu ...
- signal( SIGINT, SigIntHandler )
signal 的第1个参数signum表示要捕捉的信号,第2个参数是个函数指针,表示要对该信号进行捕捉的函数,该参数也可以是SIG_DEF(表示交由系统缺省处理,相当于白注册了)或SIG_IGN(表示 ...
- CSU - 1530 Gold Rush —— 二进制
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1530 对于一块2^n质量的gold.需要把它分成a质量和b质量(a+b=2^n),且 ...
- SSL协议、HTTP和HTTPS和区别
SSL协议 SLL协议的握手过程 开始加密通信之前,客户端和服务器首先必须建立连接和交换参数,这个过程叫做握手(handshake). 第一步,客户端给出协议版本号.一个客户端生成的随机数(Clien ...
- 洛谷【P839】【NOI导刊】——数页码
题面 一道找规律好题... 首先,我们肯定只能一位一位的统计答案,考虑从高位向低位统计,显然这样要方便的多. 对于第i位,我们统计从$a[i+1]*10^i+0$到$a[i+1]*10^i+a[i]* ...
- Xamarin.Forms初始
前言 Xamarin.Forms 为 .NET 开发人员提供一个完整的跨平台 UI 工具包. 在 Visual Studio 中使用 C# 生成完全本机的 Android.iOS 和通用 Window ...