UI中的多線程

當我們打開音樂播放器聽音樂的同時,又瀏覽網頁,這兩件事情同時執行要如何做到呢,就需要運用到多線程的知識. 

線程:程序中運行的獨立代碼段,是程序真正的執行單元,每個運行的程序(即進程),至少包含一個線程,這個線程稱爲主線程.主線程在程序啓動時被創建,用於執行main函數.IOS允許用戶根據需要開闢若干子線程,子線程和主線程都是獨立的運行單元,各自執行互不影響,因此能夠併發執行.

多線程:擁有多個線程的程序1大量運算 for; 2數據讀取 a.本地讀取 b.數據庫查找所有東西 c.網絡請求

IOS中多線程實現種類有多種


注意:在多線程方法中需要添加自動釋放池

-(void)button{

 //

 一.NSThread是一種輕量級的多線程,它有兩種創建方法

//1

    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(sum) object:nil];

    [thread start];

    [thread release];

//2.

    [NSThread detachNewThreadSelector:@selector(sum) toTarget:self withObject:nil];

//

二.NSObject中存在存在了一個最簡單的後臺執行方法

    [self performSelectorInBackground:@selector(sum) withObject:self];


}


-(void)sum{

    @autoreleasepool {

        int sum = 0;

    for (int i = 0; i< 6350; i++) {

        sum = sum +i ;

    }

    NSLog(@"%d",sum);


    }

    [self performSelectorOnMainThread:@selector(createImage) withObject:@"sdfa" waitUntilDone:YES];

}




-(void)button{

    //

三.NSOperationNSOperationQueue

        NSLog(@"main therad");

        Lei *my = [[Lei alloc]init];

        //[my start];//NSOperation添加到NSOperationQueue中不能寫start

        Lei *you = [[Lei alloc] init];

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];

        queue.maxConcurrentOperationCount = 1;//最大併發數1,每次只執行一個線程,當最大併發數爲1,線程與線程同步執行

       [queue addOperation:my];

        [queue addOperation:you];



#import "Lei.h"


@implementation Lei

-(void)dealloc{

    [super dealloc];

}

-(instancetype)init{

    self = [super init];

    if (self) {

        

    }return self;

}

//NSOperation來說,如果自己繼承的,則需要在main函數中寫子線程需要實現的代碼,main函數是一旦創建對象會自動調用

-(void)main{

    @autoreleasepool {

         for (int i = 0; i < 1000; i++) {

        NSLog(@"%d",i);

    }

  }

}

@end


   //

四.系統多線程NSBlockOperation

    NSBlockOperation * block=  [NSBlockOperation blockOperationWithBlock:^{

    @autoreleasepool {

       for (int i = 0; i < 1000; i++) {

           NSLog(@"%d",i);

       }

   }

        

   }];//block裏面就是多線程所要執行的方法

    [queue addOperation:block];

//

五.GCD多線程,不能寫自動釋放池

-(void)cerateSerialGCD{

    //第一步,創建一個同步的多線程隊列

    dispatch_queue_t queue = dispatch_queue_create("frist", DISPATCH_QUEUE_SERIAL);

    //第二步,異步執行同步線程隊列

    dispatch_async(queue, ^{

        //多線程的代碼

        //下載一張照片,並顯示在界面上(同步的方式下載)

        NSURL *url = [NSURL URLWithString:@"http://v.juhe.cn/movie/picurl?2583246"];

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

        [request setHTTPMethod:@"GET"];

        NSURLResponse *response = nil;

        NSData *connection = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

        UIImage *img = [UIImage imageWithData:connection];

        

        //顯示到界面,所有跟UI有關的內容全部都要寫在主線程運行

        //返回主線程

        dispatch_async(dispatch_get_main_queue(), ^{

            UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(100, 300, 200, 200)];

            imgv.image = img;

            [self.view addSubview:imgv];

        });

        

        

    });

    

}

-(void)createConcurrentGCD{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        //

        NSURL *url = [NSURL URLWithString:@"http:\/\/v.juhe.cn\/movie\/picurl?2583246"];

        //同步下載

        NSData *data = [NSData dataWithContentsOfURL:url];

        UIImage *img = [UIImage imageWithData:data];

        

        dispatch_async(dispatch_get_main_queue(), ^{

            UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(100, 300, 200, 200)];

            imgv.image = img;

            [self.view addSubview:imgv];

        });

    });

}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章