你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS 多線程/GCD

IOS 多線程/GCD

編輯:IOS開發綜合
多線程 —單利

+ (SingleHandel *)shareModel

{

static dispatch_once_t onceQueue;

dispatch_once(&onceQueue, ^{

shareSingle = [[SingleHandel alloc] init];

});

return shareSingle;

}

/* //第一種線程開啟方式

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

//給你的線程起個名字

thread1.name = @"thread1";

//啟動線程

[thread1 start];

//關閉

[thread1 cancel];*/

//第二種線程開啟方式(程序運行的時候自動開啟線程)

/*[NSThread detachNewThreadSelector:@selector(threadAction) toTarget:self withObject:nil];*/

//第三種繼承與NSObjet

// [self performSelectorInBackground:@selector(threadAction) withObject:nil];


// NSOperation

NSInvocationOperation *inOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(invocation) object:nil];

NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{

NSLog(@"我是block");

}];

// [blockOp start];

//創建隊列

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

//設置最大同時執行數量

queue.maxConcurrentOperationCount = 1;

//添加事件

[queue addOperation:inOp];

// sleep(1);

[queue addOperation:blockOp];

}


- (void)invocation

{

// NSLog(@"%@",[NSThread currentThread]);

NSLog(@"我是方法");

}




/-----------------------------------------------------GCD----------------------------------------------------------------/

多線程---GCD

- (IBAction)buttonAction:(id)sender

{

//GCD(先進先出first in first out 簡稱FIFO)

//串行 前一個執行完成之後,後一個任務才能執行

//並行 任務在派發時時有序的,不用等到第一個任務執行完成才開始下一個(也就是一塊執行)

//GCD分為三種:主隊列,全局隊列,自定義隊列

/*--------------------主隊列(串行)----------------------*/

//使用主隊列實現任務派發(串行),在主線程中

/*dispatch_queue_t mainQueue = dispatch_get_main_queue();

//添加任務

dispatch_async(mainQueue, ^{

NSLog(@"第一個任務%@",[NSThread currentThread]);


});

dispatch_async(mainQueue, ^{

NSLog(@"第二個任務:%@",[NSThread currentThread]);

});

dispatch_async(mainQueue, ^{

NSLog(@"第三個任務:%@",[NSThread currentThread]);

});*/

/*--------------------自定義隊列(串行)----------------------*/

//自定義隊列

/* dispatch_queue_t myQueue = dispatch_queue_create("com.myqueue", DISPATCH_QUEUE_SERIAL);

//添加任務

dispatch_async(myQueue, ^{

NSLog(@"第一個任務%@",[NSThread currentThread]);

});

dispatch_async(myQueue, ^{

sleep(3);

NSLog(@"第二個任務:%@",[NSThread currentThread]);

});

dispatch_async(myQueue, ^{

NSLog(@"第三個任務:%@",[NSThread currentThread]);

});*/

/*--------------------自定義隊列(並行)----------------------*/

/* dispatch_queue_t myqueue1 = dispatch_queue_create("com.s", DISPATCH_QUEUE_CONCURRENT);

dispatch_async(myqueue1, ^{

NSLog(@"第一個任務%@",[NSThread currentThread]);

});

dispatch_async(myqueue1, ^{

//可以延時

sleep(3);

NSLog(@"第二個任務%@",[NSThread currentThread]);

});

dispatch_async(myqueue1, ^{

NSLog(@"第三個任務%@",[NSThread currentThread]);

});*/

/*--------------------全局隊列(並行)----------------------*/

/* dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(globalQueue, ^{

sleep(2);

NSLog(@"第一個任務:%@",[NSThread currentThread]);

});

dispatch_async(globalQueue, ^{

NSLog(@"第二個任務:%@",[NSThread currentThread]);

});

dispatch_async(globalQueue, ^{

NSLog(@"第三個任務:%@",[NSThread currentThread]);

});*/

/*---------------------------------延時---------------------------------------------*/

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

[self afther];

});

/*-----------------------------重復執行---------------------------------------------*/

dispatch_apply(3, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t t) {

[self afther];


});

/*-----------------------------執行一次---------------------------------------------*/

static dispatch_once_t onceQueue ;

dispatch_once(&onceQueue, ^{

NSLog(@"我就執行一次");

});

/*----------------------------------線程通訊---------------------------------*/

//遇到線程通訊,執行他的方法,可以回到主線程(如果正在執行子線程,走這個方法可以跳出子線程到主線程)

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

}

- (void)newThread

{

NSLog(@"我是子線程");

//回到主線程

[self performSelectorOnMainThread:@selector(afther) withObject:self waitUntilDone:NO];

}

-(void)afther

{

NSLog(@"sad");

}


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved