使用NSThread起线程

 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,很多网上文章说,不能调这个大小,那是以讹传讹,如何调大小,请看我的代码。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值