//
// RunLoopTest.m
import "RunLoopTest.h"
import <pthread.h>
import <CoreFoundation/CFRunLoop.h>
@interface RunLoopTest() {
NSRunLoop * loop;
}
@end
@implementation RunLoopTest
- (instancetype)init {
if (self = [super init]) {
// [self createThread];
// NSLog(@"并行加同步");
// [self DispatchQueueConcurrentAndSync];
// [self DispatchQueueConcurrentAndAsync];
// NSLog(@"串行加同步");
// [self DispatchQueueSerialAndSync];
// [self mainQueueAndAsync];
// [self dispatchConnect];
// [self dispatchBarrierAsync];
[self dispatchGroup];
}
return self;
}
-
(void)createThread {
// 1.Pthreads
// pthread_t thread;
// pthread_create(&thread, NULL, start, NULL);
// 2.NSThread
// NSThread * thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(thread1Run) object:nil];
// [thread1 start];
// 3.GCD
dispatch_queue_t queue1 = dispatch_queue_create("test.queue", DISPATCH_QUEUE_SERIAL);//串行隊列
dispatch_queue_t queue2 = dispatch_queue_create("test2.queue", DISPATCH_QUEUE_CONCURRENT);//并行隊列
dispatch_sync(queue1, ^{
// NSLog(@"同步執(zhí)行");
});//不會開辟新的線程dispatch_async(queue1, ^{
// NSLog(@"異步執(zhí)行");
});//會開辟新的線程
}
pragma mark -- 并行隊列 + 同步執(zhí)行
- (void)DispatchQueueConcurrentAndSync {
dispatch_queue_t queue1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//采用全局并行隊列
dispatch_sync(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"1------%@",[NSThread currentThread]);
}
});
dispatch_sync(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"2------%@",[NSThread currentThread]);
}
});
dispatch_sync(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"3------%@",[NSThread currentThread]);
}
});
//可以預(yù)測到三個線程會按編碼的順序依次執(zhí)行字币,并且都是在主線程上執(zhí)行
NSLog(@"syncConcurrent---end");
}
pragma mark -- 并行隊列 + 異步執(zhí)行
- (void)DispatchQueueConcurrentAndAsync {
dispatch_queue_t queue1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//采用全局并行隊列
dispatch_async(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"1------%@",[NSThread currentThread]);
}
});
dispatch_async(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"2------%@",[NSThread currentThread]);
}
});
dispatch_async(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"3------%@",[NSThread currentThread]);
}
});
//可以預(yù)測到先打印的是@"asyncConcurrent---end"洗出,因為這個方法是在主線程上執(zhí)行,等級高于異步開的子線程翩活,然后打印3個子線程菠镇,并且三個子線程打印的順序是不固定,因為是并行隊。所以異步線程并不影響主線程
NSLog(@"asyncConcurrent---end");
}
pragma mark -- 串行隊列 + 異步執(zhí)行
- (void)DispatchQueueSerialAndAsync {
// 會開啟新線程辟犀,但是因為任務(wù)是串行的,執(zhí)行完一個任務(wù)魂毁,再執(zhí)行下一個任務(wù)
dispatch_queue_t queue1 = dispatch_queue_create("test1.queue1", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"1------%@",[NSThread currentThread]);
}
});
dispatch_async(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"2------%@",[NSThread currentThread]);
}
});
dispatch_async(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"3------%@",[NSThread currentThread]);
}
});
//可以預(yù)測到先打印的是@"asyncConcurrent---end"出嘹,因為這個方法是在主線程上執(zhí)行税稼,等級高于異步開的子線程垮斯,然后打印按順序依次執(zhí)行3個子線程里面的方法只祠,并且三個子線程打印順序是固定,因為是串行隊列抛寝。所以異步線程并不影響主線程
NSLog(@"asyncSerial---end");
}
pragma mark -- 串行隊列 + 同步執(zhí)行
- (void)DispatchQueueSerialAndSync {
// 不會開啟新線程盗舰,執(zhí)行完一個任務(wù),再執(zhí)行下一個任務(wù)
dispatch_queue_t queue1 = dispatch_queue_create("test1.queue1", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"1------%@",[NSThread currentThread]);
}
});
dispatch_sync(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"2------%@",[NSThread currentThread]);
}
});
dispatch_sync(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"3------%@",[NSThread currentThread]);
}
});
NSLog(@"syncSerial---end");
//可以預(yù)測到三個線程會按編碼的順序依次執(zhí)行川陆,并且都是在主線程上執(zhí)行
}
pragma mark -- 主隊列 + 同步執(zhí)行
-
(void)mainQueueAndSync {
NSLog(@"syncMain---begin");// 互等卡住不可行(在主線程中調(diào)用)
dispatch_queue_t queue1 = dispatch_get_main_queue();
dispatch_sync(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"1------%@",[NSThread currentThread]);
}
});
dispatch_sync(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"2------%@",[NSThread currentThread]);
}
});
dispatch_sync(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"3------%@",[NSThread currentThread]);
}
});
NSLog(@"syncMain---end");
//可以預(yù)測到三個線程會按編碼的順序依次執(zhí)行蛮位,并且都是在主線程上執(zhí)行
}
pragma mark -- 主隊列 + 異步執(zhí)行
-
(void)mainQueueAndAsync {
NSLog(@"asyncMain---begin");dispatch_queue_t queue1 = dispatch_get_main_queue();
dispatch_async(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"1------%@",[NSThread currentThread]);
}
});
dispatch_async(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"2------%@",[NSThread currentThread]);
}
});
dispatch_async(queue1, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"3------%@",[NSThread currentThread]);
}
});
NSLog(@"asyncMain---end");
}
pragma mark -- GCD線程之間的通訊
-
(void)dispatchConnect {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i = 0; i < 2; i ++) {
NSLog(@"1------%@",[NSThread currentThread]);
}
dispatch_async(dispatch_get_main_queue(), ^{
for (int i = 0; i < 2; i ++) {
NSLog(@"2------%@",[NSThread currentThread]);
}
});
});
// 先進(jìn)行子線程的操作土至,然后回到主線程進(jìn)行下一步操作
}
pragma mark -- GCD的柵欄方法 dispatch_barrier_async
-
(void)dispatchBarrierAsync {
dispatch_queue_t queue = dispatch_queue_create("12323", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"1------%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2------%@",[NSThread currentThread]);
});dispatch_barrier_async(queue, ^{
NSLog(@"等一會在進(jìn)行后面的答應(yīng)---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3------%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"4------%@",[NSThread currentThread]);
});
}
pragma mark -- GCD的隊列組 dispatch_group
-
(void)dispatchGroup {
dispatch_group_t group1 = dispatch_group_create();
dispatch_group_async(group1, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//耗時操作1
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
loop = [NSRunLoop currentRunLoop];
[loop addTimer:timer forMode:NSRunLoopCommonModes];
[loop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10.0]]; //主線程等待陶因,但讓出主線程時間片,然后過10秒后返回});
dispatch_group_async(group1, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//耗時操作2
NSTimer * timer1 = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
loop = [NSRunLoop currentRunLoop];
[loop addTimer:timer1 forMode:NSRunLoopCommonModes];
[loop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0]]; //主線程等待楷扬,但讓出主線程時間片烘苹,然后過10秒后返回});
// 等上面的耗時操作都完成了,再回到主線程dispatch_group_notify(group1, dispatch_get_main_queue(), ^{
NSLog(@"3");
});
} (void)timerAction {
NSLog(@"%@",[NSThread currentThread]);
}(void)thread1Run {
}
//static CFSpinlock_t loopsLcok;
@end