进程:程序在计算机的一次运行活动,一个程序就是一个进程,在iOS中一个app就是一个进程
线程:程序运行的最小单元,一个进程中至少有一个线程(主线程)
一. 通过NSObject的方法管理线程   
       这种方法会帮助我们自己主动开辟一个后台线程,不须要自己创建   
       參数:(1)在这个后台线程中运行的方法   
    參数:(2)用于传递參数,没有为nil   
[self
performSelectorInBackground:@selector(banZhuanPlus)
withObject:nil];
}
    1.创建线程   
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(banZhuanPlus) object:nil];
    2.运行   
      [thread start];   
[thread release];
          [self
banZhuanPlus];   
}];        
2.创建队列
队列会自己主动创建一个辅助的线程;NSOperationQueue *queue = [[NSOperationQueue
alloc]
init];       
         最大并行数设置,同一时候运行几个操作单元   
    [queue setMaxConcurrentOperationCount:1];   
    3.运行,仅仅要把操作单元加入?到队列中,它就会运行   
    [queue addOperation:block];   
[queue addOperation:invocation];
          第一种队列   
  dispatch_queue_t mainQueue =
dispatch_get_main_queue();//主调队列,在主线程中运行,而且是串行(一次操作一个)   
     
          另外一种队列   
          全局队列,在子线程中运行,而且是并行(一次能够运行多个);   
          參数:(1)设置队列的优先级(high, default,low,background);(2)预留參数,未来使用      
     dispatch_queue_t globalQueue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0);   
          第三种队列   
         自己定义队列,在子线程中运行,能够设置并行或者串行   
         參数:(1)区分队列的唯一标识可选项,假设不写:NULL;假设写,规范样例”com.example.myqueue".   
         參数:(2)设置并行或者串行的
并行 :DISPATCH_QUEUE_CONCURRENT;
串行:DISPATCH_QUEUE_SERIAL(or NULL)   
dispatch_queue_t customQueue = dispatch_queue_create("com.example.myqueue", DISPATCH_QUEUE_CONCURRENT);
第一种,同步运行(须要等待运行完成)
參数:(1)指定队列,(2)Block,运行的操作 
   
      dispatch_sync(globalQueue, ^{       
[self banZhuanPlus];//运行的操作
});另外一种,异步运行(无需等待)   
    dispatch_async(globalQueue, ^{      
[self banZhuanPlus];//运行的操作
});dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
 (int64_t)(2 *
NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{       
        [self
banZhuanPlus];  
});
五.线程中注意点
第一,假设是在MRC模式下,线程中的autorelease对象不能释放,必须手动释放,或者加入?自己主动释放池
原文:http://www.cnblogs.com/hrhguanli/p/4003610.html