多線程:
多線程是爲了同步完成多項任務,不是爲了提高運行效率,而是爲了提高資源使用效率來提高系統的效率。線程是在同一時間需要完成多項任務的時候實現的.
多線程包括:GCD NSThread NSOperation NSOperation是在GCD語言的基礎上開發的,GCD類C語言,NSThread
NSOperation OC語法
iOS允許用戶自己開闢新的線程,相對於主線程來說,這些線程稱作子線程,子線程和主線程都是獨立的運行單元,各自的執行不影響,因此能夠併發執行.
實例
首先創建一個button
- (void)button
{
self.button1 = [[UIButtonalloc] initWithFrame:CGRectMake(50,50, 50, 50)];
[_button1setTitle:@"點擊"forState:UIControlStateNormal];
[_button1setTitleColor:[UIColorredColor] forState:UIControlStateNormal];
[_button1setTitleColor:[UIColorredColor] forState:UIControlStateHighlighted];
[self.viewaddSubview:_button1];
[_button1 release];
}
- (void)buttonActions:(id)sender
{
// 每一個跑多線程的地方都加釋放池
@autoreleasepool {
int k = 0;
for (int i =1; i < 63; i++) {
k += i;
NSLog(@"%d", k);
}
}
//NSObect,NSThread,NSOperation返回主線程方式
[selfperformSelectorOnMainThread:@selector(createImage:)withObject:@"asdf"waitUntilDone:YES];
}
- (IBAction)buttonAction:(id)sender {
#warning NSThread
//方法中object是往形參上傳值的
// 輕量級多線程
//創建NSThread
NSThread * thread = [[NSThreadalloc] initWithTarget:selfselector:@selector(buttonActions:)object:nil];
[thread start];
[thread release];
//第二種創建方法
[NSThreaddetachNewThreadSelector:@selector(buttonActions:)toTarget:selfwithObject:nil];
}
- (IBAction)buttonAction:(id)sender
{
#warning NSObject
//創建方法
[self performSelectorInBackground:@selector(buttonActions:) withObject:nil];
}
NSOperationQueue操作隊列
NsOperation是抽象的類,所以不能直接使用這個類,而是要使用這個類的子類
//首先創建一個繼承與NsOperation的類NsOperationOne
//創建NsOperationOne的對象
NsOperationOne * one = [[NsOperationOnealloc] init];
//NSOperationQueue 會自動調用NsOperationOne的start方法,所以不寫
//創建NSOperationQueue的對象
NSOperationQueue * que = [[NSOperationQueuealloc] init];
// 設置NSOperationQueue的最大併發線程個數
[que setMaxConcurrentOperationCount:1];
[queaddOperation:one];
[onerelease];
//線程併發,順序不清,互不干擾
//iOS同時運行的多線程不超過10個,需要要限制,最大併發數爲1的時候線程與線程之間是同步的(等一個線程做完以後再執行下一個)
//線程同步serial線程併發concurrent
NsOperationOne * second = [[NsOperationOnealloc]init];
[queaddOperation:second];
[secondrelease];
[que release];
// NSBlockOperation NSInvocationOperation是NsOperation的子類
//block裏面的內容就是多線程所要執行的代碼
NSBlockOperation * block = [NSBlockOperation blockOperationWithBlock:^{
@autoreleasepool {
for (int i =0; i < 10; i++) {
NSLog(@"%d", i);
}
}
}];
NSInvocationOperation * invo = [[NSInvocationOperation alloc]initWithTarget:selfselector:@selector(buttonActions:) object:nil];
[queueaddOperation:invo];
[invorelease];
[queuerelease];
#pragma mark GCD多線程
//同步線程隊列
- (void)createSerialGCD
{
//第一步:創建一個同步線程隊列
//C語言第一個參數 隊列的名字第二個參數是標記是同步隊列
dispatch_queue_t queue =dispatch_queue_create("first",DISPATCH_QUEUE_SERIAL);
//第二步:異步執行同步線程隊列(異步是與主線程異步)
dispatch_async(queue, ^{
//多線程的代碼, GCD中不能寫自動釋放池(因爲GCD是C語言)
//子線程不能修改UI界面
//下載一張圖片,並顯示到主界面(下載用同步下載)
NSString * urlStr =@"http://img.hb.aicdn.com/258541cf3d0b5dc5c68f9f3096a830b68288d2011f65f-00EuvV_fw658";
//網址
NSURL * url = [NSURLURLWithString:urlStr];
NSMutableURLRequest * req = [NSMutableURLRequestrequestWithURL:url];
[reqsetHTTPMethod:@"GET"];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnectionsendSynchronousRequest:req returningResponse:&response error:&error];
UIImage * image = [UIImageimageWithData:data];
//顯示到界面上,所有跟UI有關的內容都要到主線程運行
//返回主線程
dispatch_async(dispatch_get_main_queue(), ^{
//在主線程運行
UIImageView * imageV = [[UIImageViewalloc] initWithFrame:CGRectMake(250,350, 100, 150)];
imageV.image = image;
[self.viewaddSubview:imageV];
[imageVrelease];
});
});
}
//*******
//並行隊列
- (void)createConnectGCD
{
//第一個參數:級別
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
NSURL * url = [NSURLURLWithString:@"http://h.hiphotos.baidu.com/image/pic/item/241f95cad1c8a786beb13e066509c93d70cf501a.jpg"];
//數據
NSData * data = [NSDatadataWithContentsOfURL:url];
//照片
UIImage * image = [UIImageimageWithData:data];
//返回主線程
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView * imageV = [[UIImageViewalloc] initWithFrame:CGRectMake(50,100, 100, 150)];
imageV.image = image;
[self.viewaddSubview:imageV];
[imageVrelease];
});
});
}