IOSラーニングのマルチスレッド(2)-スレッドの作成


転載先http://www.cnblogs.com/wendingding/p/3805119.html
一、スレッドの作成と起動の簡単な説明
NSThreadオブジェクトはスレッドを表します
スレッドの作成、開始
(1) NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start];
//スレッドが起動すると、スレッドthreadでselfのrunメソッドが実行されます
 
プライマリ・スレッド関連の使用法
+ (NSThread *)mainThread;//マスタースレッド取得
- (BOOL)isMainThread;//メインスレッドか
+ (BOOL)isMainThread;//メインスレッドか
 
その他の使い方
現在のスレッドを取得
NSThread *current = [NSThread currentThread];
 
スレッドのスケジューリング優先度:スケジューリング優先度の値範囲は0.0~1.0で、デフォルトは0.5で、値が大きいほど優先度が高くなります.
+ (double)threadPriority;
+ (BOOL)setThreadPriority:(double)p;
 
スレッド名の設定
- (void)setName:(NSString *)n;
- (NSString *)name;
 
スレッドの作成方法
(2)スレッド作成後にスレッドを自動的に起動する[NSThread detachNewThreadSelector:@selector(run)toTarget:self withObject:nil];
(3)スレッドを暗黙的に作成して起動する[self p e r f f e r f f r o m e f SelectorInBackground:@selector(run)withObject:nil];
上記2つのスレッド作成方式のメリットとデメリット
利点:シンプルでショートカット
欠点:スレッドをより詳細に設定できません
二、コード例
1.古い方法で作成
//
//  YYViewController.m
//
//
//  Created by apple on 14-6-23.
//  Copyright (c) 2014  itcase. All rights reserved.
//
#import "YYViewController.h"
#import <pthread.h>
@interface YYViewController ()
- (IBAction)btnClick;
@end
 
@implementation YYViewController
 
- (void)viewDidLoad
{
    [super viewDidLoad];
}
 
//       
- (IBAction)btnClick {
    //1.      
    NSThread *current=[NSThread currentThread];
    //   
    NSLog(@"btnClick----%@",current);   
    //2.  for          
   pthread_t thread;
    pthread_create(&thread, NULL, run, NULL);
}
//c    
void *run(void *data)
{
    //      ,         
    NSThread *current=[NSThread currentThread];
    for (int i=0; i<10000; i++) {
        NSLog(@"btnClick---%d---%@",i,current);
    }
    return NULL;
}
//    ,               ,        
@end

効果:

 
印刷結果:

2.NSThreadを使用したスレッドの作成
//
//  YYViewController.m
//
//
//  Created by apple on 14-6-23.
//  Copyright (c) 2014  itcase. All rights reserved.
//
#import "YYViewController.h"
#import <pthread.h>
@interface YYViewController ()
- (IBAction)btnClick;
@end
@implementation YYViewController 
- (void)viewDidLoad
{
    [super viewDidLoad];
}
//       
- (IBAction)btnClick {
    //1.      
    NSThread *current=[NSThread currentThread];
    //   
    NSLog(@"btnClick----%@",current);
    //            
   NSThread *main=[NSThread mainThread];
    NSLog(@"   -------%@",main);
    //2.        
    [self creatNSThread];
//    [self creatNSThread2];
//    [self creatNSThread3];
}
 
/**
 * NSThread      1
 * 1>         
 * 2> start    
 */
-(void)creatNSThread
{
    NSThread  *thread=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"  A"];
    //         
    thread.name=@"  A";
     //    
    [thread start];
  
    NSThread  *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"  B"];
    //         
    thread2.name=@"  B";
   //    
    [thread2 start];
}
 
/**
 * NSThread      2
*       (  )  
 */
-(void)creatNSThread2
{
//    NSThread *thread=[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"       (  )  "];
    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"       (  )  "];
}
/**
 * NSThread      3
 *       ,     (  )  
 */
-(void)creatNSThread3
{
    //        ===       
    [self performSelectorInBackground:@selector(run:) withObject:@"    "];
}
-(void)run:(NSString *)str
{
   //      
    NSThread *current=[NSThread currentThread];
    //    
    for (int i=0; i<10; i++) {
       NSLog(@"run---%@---%@",current,str);
    }
}
@end

スレッド1を呼び出し、印刷結果は次のとおりです.

呼び出しスレッド2

呼び出しスレッド3