多线程个人技术总结

//
//  AppDelegate.m
//  ThreadDemo
//
//  Created by wei.chen on 13-1-10.
//  Copyright (c) 2013 www.iphonetrain.com 无限互联 3G 学院 . All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate

- (
void )dealloc
{
    [
_window release ];
    [
super dealloc ];
}

- (
BOOL )application:( UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions
{
   
self . window = [[[ UIWindow alloc ] initWithFrame :[[ UIScreen mainScreen ] bounds ]] autorelease ];
   
self . window . backgroundColor = [ UIColor whiteColor ];
    [
self . window makeKeyAndVisible ];
   
   
   
//----------1. 第一种方式:创建多线程对象
//    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(mutableThread:) object:@"test"];
//    // 开始运行多线程
//    [thread start];
   
   
   
//-----------2. 第二种方式
//    [NSThread detachNewThreadSelector:@selector(mutableThread:) toTarget:self withObject:nil];
   
   
   
//-----------3. 第三种方式
//    [self performSelectorInBackground:@selector(mutableThread:) withObject:nil];
//    [self mutableThread:nil];
   
   
//-----------4. 第四种方式
//    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
//    [operationQueue addOperationWithBlock:^{
//        for (int i=0; i<100; i++) {
//            NSLog(@"-- 多线程 -%d",i);
//        }
//    }];
   
   
//-----------5. 第五种方式
   
// 创建一个线程队列
//    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
//    // 设置线程执行的并发数
//    operationQueue.maxConcurrentOperationCount = 1;
//   
//    // 创建一个线程操作对象
//    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(mutableThread:) object:nil];
//   
//    // 创建一个线程操作对象
//    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(mutableThread2:) object:nil];
//    // 设置线程的优先级
//    operation2.queuePriority = NSOperationQueuePriorityHigh;
//   
//    // 将线程添加到线程队列中
//    [operationQueue addOperation:operation1];
//    [operationQueue addOperation:operation2];
   
   
//-----------------6. 第六种方式: GCD
   
// 创建一个队列
   
dispatch_queue_t queue = dispatch_queue_create ( "test" , NULL );
   
dispatch_async (queue, ^{
       
for ( int i= 0 ; i< 100 ; i++) {
           
NSLog ( @"-- 多线程 1-%d" ,i);
        }
       
       
BOOL isMuliti = [ NSThread isMultiThreaded ];
       
if (isMuliti) {
           
NSLog ( @" 多线程 " );
        }
       
       
dispatch_sync ( dispatch_get_main_queue (), ^{
           
BOOL isMain = [ NSThread isMainThread ];
           
if (isMain) {
               
NSLog ( @" 主线程 " );
            }
        });
       
    });
   
   
// 通过此种方式,还是同步运行在当前线程上
   
dispatch_sync (queue, ^{
       
// 当前线程
    });
   
   
   
for ( int i= 0 ; i< 100 ; i++) {
       
NSLog ( @"-- 主线程 -%d" ,i);
    }   
   
   
return YES ;
}


- (
void )mutableThread:( NSString *)t {
   
for ( int i= 0 ; i< 100 ; i++) {
       
NSLog ( @"-- 多线程 1-%d" ,i);
    }
   
   
   
// 跳到主线程执行
    [
self performSelectorOnMainThread : @selector (mainThread) withObject : nil waitUntilDone : YES ];
   
   
//...
}

- (
void )mutableThread2:( NSString *)t {
   
for ( int i= 0 ; i< 100 ; i++) {
       
NSLog ( @"-- 多线程 2-%d" ,i);
    }
}


- (
void )mainThread {
   
// 判断当前线程是否为主线程
   
BOOL isMain = [ NSThread isMainThread ];
   
if (isMain) {
       
NSLog ( @"mainThread" );
    }
}

@end

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值