NSThread是起线程的主角,大部分时候我们使用这个类。
由于本人macbook暂时不能用,所以这里只贴出代码:
建一个view-based application.
在viewcontroller.h中
@interface tNSThreadViewController : UIViewController {
NSThread *t1;
BOOL bExit;
}
@property BOOL bExit;
- (IBAction)checkit:(id)sender;
- (IBAction)start2:(id)sender;
- (IBAction)start:(id)sender;
-(void)doSomething:(id)data;
@end
在viewcontroller.m中
#import "tNSThreadViewController.h"
@implementation tNSThreadViewController
@synthesize bExit;
-(void)doSomething:(id)data
{
/*[[t1 threadDictionary] setObject:@"kkk" forKey:@"k11"];
NSMutableDictionary * md = [t1 threadDictionary];
NSString *t = [md objectForKey:@"k11"];
NSLog(@"%@", t);
NSLog(@"kasdkf");*/
//NSString *d = (NSString *)data;
//NSLog(@"%@",d);
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *s = [NSString stringWithFormat:@"%d",1];
NSLog(@"%@",s);
while (true) {
[NSThread sleepForTimeInterval:0.5];
NSLog(@"IN thread");
tNSThreadViewController *inputData = (tNSThreadViewController *)data;
if(inputData.bExit)
break;
}
[pool release];
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
bExit = NO;
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)checkit:(id)sender {
bExit = YES;
if([NSThread isMultiThreaded]) {
NSLog(@"it is multi thread");
}
else {
NSLog(@"it is not multi thread");
}
}
- (IBAction)start2:(id)sender {
if(t1 != nil)
{
[t1 release];
t1 = nil;
}
t1 = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething:) object:self];
[t1 setStackSize:1024*1024];
[t1 setThreadPriority:0.5];
[t1 start];
/*NSThread *t2 = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething:) object:@"2"];
[t2 setStackSize:1024*1024];
[t2 setThreadPriority:0.9];
[t2 start];*/
}
- (IBAction)start:(id)sender {
//[self performSelectorInBackground:<#(SEL)#> withObject:<#(id)#>
[NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:nil];
}
@end
最后需要说明的是,iOS应用的主线程的栈大小缺省为1M,其他线程的栈大小缺省为512K,很多网上文章说,不能调这个大小,那是以讹传讹,如何调大小,请看我的代码。
分享到:
相关推荐
NSThread 线程的创建,交互,及常用的方法
别人的,共享给大家,是介绍NSThread的使用,对源码编写这表示感谢
iOS多线程开发一 使用NSThread NSThread的基本使用 // demo说明 NSThreadDemoOne: 简单使用多线程,区分有多线程和没有多线的区别 NSThreadDemoTwo: 因为NSThread只能传一个一个参数,如果咬传递多个参数,使用封装...
swift NSThread线程同步买票小例子
这个文档是我在其他地方找到的,感觉对于初学者了解ios开发的线程使用很有帮助,在此分享一下.
ios 开辟分线程,通过nsthread开辟了一个新的线程
该demo详细解释了gcd NSThread NSOperationQueue的用法.详细解释了子线程中如何刷新UI
iOS多线程,四种实现方式中最简单的两种pthread,NSThread的用法介绍。
iOS多线程开发NSThread.pdf
iOS多线程编程之Grand Central Dispatch(GCD)使用,卖票的例子来讲NSThread的线程同步,使用了两种锁,一种NSCondition ,一种是:NSLock,还有加了一个 线程3 去唤醒其他两个线程锁中的wait,里面的注释很已经写的...
##通过使用NSThread类来讲解iOS中的多线程,创建了12个线程,来下载12张图片,而且把最后一个线程的优先级设置成最高,其他线程在最后一个线程执行完毕之后在执行 ##ViewController.m ###1.点击按钮下载的方法来开始...
在iOS开发中,多线程的实现方式主要有三种,NSThread、NSOperation和GCD,我前面博客中对NSOperation和GCD有了较为详细的实现,为了学习的完整性,今天我们主要从代码层面来实现NSThread的使用。案例代码上传至 ...
IOS多线程编程NSThread的使用方法 NSThread是多线程的一种,有两种方法创建子线程 (1)优点:NSThread 比GCD、NSOperation都轻量级 (2)缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有...
iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD
iOS多线程编程之NSThread的使用案例源码,1、简介:1.1 iOS有三种多线程编程的技术,分别是:1.、NSThread 。2、Cocoa NSOperation (iOS多线程编程之NSOperation和NSOperationQueue的使用)3、GCD 全称:Grand ...
ios多线程编程指南,包含NSThread,CGD等,深入简出讲解ios多线程原理和应用
IOS应用源码——ui多线程的简单例子 NSThread.zip
那什么时候需要加锁呢,就是当多条线程同时操作一个变量时,就需要加锁了。 上代码 声明变量 @interface ViewController () @property (strong, nonatomic)NSThread *thread1; @property (strong, nonatomic)...
多线程加载图片
ios多线程开发的常用四种方式和基本使用,pthread、NSThread、NSOperation\NSOperationQueue、GCD