加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

NS类 - NSThread几种创建方法

(2013-05-13 11:25:08)
标签:

nsthread

it

分类: Mac/IOS那些事
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
//第一种多线程的方法, 直接来创建
    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(mutableThread:) object:@"mutable"];
    [thread start];
    for(int i=0;i<100;i++)
    {
        NSLog(@"maint--:%d",i);
    }
//第二种 直接调用nsthread的类方法
   [NSThread detachNewThreadSelector:@selector(mutableThread:) toTarget:self withObject:nil];
// 第三种
    [self performSelectorInBackground:@selector(mutableThread:) withObject:nil];
//第四种 直接添加一个block再子线程上 这样不用单独写方法 其实这个是创建了一个线程池 叫线程队列
//可以添加多个
  NSOperationQueue *threadQueue = [[NSOperationQueue alloc]init];
    threadQueue addOperationWithBlock:^{
        for(int i=0;i<100;i++)
        {
           NSLog(@"thread --:%d",i);
        }
    }
    
//第五种在 operationQueue队列中添加多个线程 ui的操作都是在主线程的
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];
    //设置最大的线程数
    operationQueue.maxConcurrentOperationCount = 2;
    
    NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(mutableThread:) object:nil];
    NSInvocationOperation *invocationOperation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(mutableThread2:) object:nil];
    //设置优先级
    invocationOperation2.queuePriority = NSOperationQueuePriorityHigh;
    
    [operationQueue addOperation:invocationOperation];
    [operationQueue addOperation:invocationOperation2];
//回到主线程 因为ui不能在子线程执行
    [self performSelectorOnMainThread:@selector(backToMain) withObject:nil waitUntilDone:YES];
    
//第六种使用GCD
    dispatch_queue_t GCDQueue =  dispatch_queue_create("GCDTest", nil);
    dispatch_async(GCDQueue, ^{
        for(int i=0;i<100;i++)
        {
            NSLog(@"GCDthread --:%d",i);
        }
        BOOL isMulti = [NSThread isMultiThreaded];
        if(isMulti){
            NSLog(@"isMutalThread!");
                   }
        dispatch_sync(dispatch_get_main_queue(), ^{
        BOOL isMain = [NSThread isMainThread];
            if (isMain) {
                NSLog(@"backToMainThread ..");
            }
        });

    });
    return YES;
}
//线程的入口方法 类似于main的功能  这个其实是有返回值的 return到调用的地方
//一般在入口方法需要添加自动释放池 道理和main函数一样
-(void)mutableThread:(NSString *)mutable
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    for(int i=0;i<100;i++)
    {
        NSLog(@"thread --:%d",i);
    }
    [pool release];
}
-(void)mutableThread2:(NSString *)mutable
{
    for(int i=0;i<100;i++)
    {
        NSLog(@"thread 2--:%d",i);
    }
}
//判断是否为主线程
-(void)backToMain
{
    BOOL isMain = [NSThread isMainThread];
    if (isMain) {
         NSLog(@"backTo MAin ");
    }
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有