├── .gitignore ├── HJSynchronize.podspec ├── HJSynchronize ├── HJSynchronize.h ├── HJSynchronizeQueue.h ├── HJSynchronizeQueue.m ├── HJSynchronizeSerial.h └── HJSynchronizeSerial.m ├── HJSynchronizeDemo.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── haijiao.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── haijiao.xcuserdatad │ └── xcschemes │ ├── HJSynchronizeDemo.xcscheme │ └── xcschememanagement.plist ├── HJSynchronizeDemo ├── AppDelegate.h ├── AppDelegate.m ├── Base.lproj │ ├── LaunchScreen.xib │ └── Main.storyboard ├── Images.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Info.plist ├── ViewController.h ├── ViewController.m └── main.m ├── HJSynchronizeDemoTests ├── HJSynchronizeDemoTests.m └── Info.plist ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | *.xcuserstate 3 | 4 | *.xccheckout 5 | 6 | *.xcuserstate 7 | -------------------------------------------------------------------------------- /HJSynchronize.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | 3 | s.name = 'HJSynchronize' 4 | s.version = '0.0.1' 5 | s.summary = 'A short description' 6 | s.homepage = "https://github.com/panghaijiao/HJSynchronize" 7 | 8 | s.license = 'MIT' 9 | 10 | s.author = { 'panghaijiao' => '275742376@qq.com' } 11 | 12 | s.platform = :ios, '8.0' 13 | 14 | s.source = { :git => "https://github.com/panghaijiao/HJSynchronizeDemo.git", :tag => "0.0.1" } 15 | 16 | s.source_files = 'HJSynchronize/**/*.{h,m}' 17 | 18 | end 19 | -------------------------------------------------------------------------------- /HJSynchronize/HJSynchronize.h: -------------------------------------------------------------------------------- 1 | // 2 | // HJSynchronize.h 3 | // HJSynchronizeDemo 4 | // 5 | // Created by Haijiao on 15/7/9. 6 | // Copyright (c) 2015年 olinone. All rights reserved. 7 | // 8 | 9 | 10 | #import "HJSynchronizeSerial.h" 11 | #import "HJSynchronizeQueue.h" 12 | 13 | static inline void onMainThreadAsync(void (^block)()) 14 | { 15 | if ([NSThread isMainThread]) block(); 16 | else dispatch_async(dispatch_get_main_queue(), block); 17 | } 18 | 19 | static inline void onMainThreadSync(void (^block)()) 20 | { 21 | if ([NSThread isMainThread]) block(); 22 | else dispatch_sync(dispatch_get_main_queue(), block); 23 | } 24 | 25 | static inline void onGlobalThreadAsync(void (^block)()) 26 | { 27 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block); 28 | } 29 | 30 | static inline void onMainThreadDelayExec(NSTimeInterval second, void (^block)()) 31 | { 32 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(second * NSEC_PER_SEC)), dispatch_get_main_queue(), block); 33 | } 34 | -------------------------------------------------------------------------------- /HJSynchronize/HJSynchronizeQueue.h: -------------------------------------------------------------------------------- 1 | // 2 | // HJSynchronizeQueue.h 3 | // HJSynchronizeManager 4 | // 5 | // Created by haijiao on 15/7/7. 6 | // Copyright (c) 2015年 olinone. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface HJSynchronizeQueue : NSOperationQueue 12 | 13 | + (void)execSyncBlock:(void (^)())block; 14 | + (NSOperation *)execAsynBlock:(void (^)())block; 15 | + (void)cancelAllOperations; 16 | 17 | - (void)execSyncBlock:(void (^)())block; 18 | - (NSOperation *)execAsynBlock:(void (^)())block; 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /HJSynchronize/HJSynchronizeQueue.m: -------------------------------------------------------------------------------- 1 | // 2 | // HJSynchronizeQueue.m 3 | // HJSynchronizeManager 4 | // 5 | // Created by haijiao on 15/7/7. 6 | // Copyright (c) 2015年 olinone. All rights reserved. 7 | // 8 | 9 | #import "HJSynchronizeQueue.h" 10 | 11 | @implementation HJSynchronizeQueue 12 | 13 | + (HJSynchronizeQueue *)synchronizeQueue 14 | { 15 | static HJSynchronizeQueue * _sharedInstance = nil; 16 | static dispatch_once_t oncePredicate; 17 | dispatch_once(&oncePredicate, ^{ 18 | _sharedInstance = [[self alloc] init]; 19 | }); 20 | return _sharedInstance; 21 | } 22 | 23 | - (instancetype)init 24 | { 25 | if (self = [super init]) { 26 | self.maxConcurrentOperationCount = 1; 27 | } 28 | return self; 29 | } 30 | 31 | + (void)cancelAllOperations 32 | { 33 | [[HJSynchronizeQueue synchronizeQueue] cancelAllOperations]; 34 | } 35 | 36 | #pragma mark - sync 37 | + (void)execSyncBlock:(void (^)())block 38 | { 39 | [[HJSynchronizeQueue synchronizeQueue] execSyncBlock:block]; 40 | } 41 | 42 | - (void)execSyncBlock:(void (^)())block 43 | { 44 | if (NSOperationQueue.currentQueue == self) { 45 | block(); 46 | } else { 47 | NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:block]; 48 | [self addOperations:@[operation] waitUntilFinished:YES]; 49 | } 50 | } 51 | 52 | #pragma mark - asyn 53 | + (NSOperation *)execAsynBlock:(void (^)())block 54 | { 55 | return [[HJSynchronizeQueue synchronizeQueue] execAsynBlock:block]; 56 | } 57 | 58 | - (NSOperation *)execAsynBlock:(void (^)())block 59 | { 60 | NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:block]; 61 | [self addOperation:operation]; 62 | return operation; 63 | } 64 | 65 | @end 66 | -------------------------------------------------------------------------------- /HJSynchronize/HJSynchronizeSerial.h: -------------------------------------------------------------------------------- 1 | // 2 | // HJSynchronizeSerial.h 3 | // TTPod 4 | // 5 | // Created by haijiao on 15/10/29. 6 | // 7 | // 8 | 9 | #import 10 | 11 | @interface HJSynchronizeSerial : NSObject 12 | 13 | + (void)execSyncBlock:(void (^)())block; 14 | + (void)execAsynBlock:(void (^)())block; 15 | 16 | - (void)execSyncBlock:(void (^)())block; 17 | - (void)execAsynBlock:(void (^)())block; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /HJSynchronize/HJSynchronizeSerial.m: -------------------------------------------------------------------------------- 1 | // 2 | // HJSynchronizeSerial.m 3 | // TTPod 4 | // 5 | // Created by haijiao on 15/10/29. 6 | // 7 | // 8 | 9 | #import "HJSynchronizeSerial.h" 10 | 11 | #if OS_OBJECT_USE_OBJC 12 | #define HJDispatchQueueRelease(__v) 13 | #else 14 | #define HJDispatchQueueRelease(__v) (dispatch_release(__v)); 15 | #endif 16 | 17 | @interface HJSynchronizeSerial () { 18 | dispatch_queue_t _queue; 19 | } 20 | 21 | @end 22 | 23 | @implementation HJSynchronizeSerial 24 | 25 | + (HJSynchronizeSerial *)synchronizeQueue { 26 | static HJSynchronizeSerial * _sharedInstance = nil; 27 | static dispatch_once_t oncePredicate; 28 | dispatch_once(&oncePredicate, ^{ 29 | _sharedInstance = [[self alloc] init]; 30 | }); 31 | return _sharedInstance; 32 | } 33 | 34 | - (void)dealloc { 35 | if (_queue) { 36 | HJDispatchQueueRelease(_queue); 37 | _queue = 0x00; 38 | } 39 | } 40 | 41 | - (instancetype)init { 42 | if (self = [super init]) { 43 | NSString *identifier = [@"com.ttpod.music." stringByAppendingString:[self randomBitString]]; 44 | _queue = dispatch_queue_create([identifier UTF8String], DISPATCH_QUEUE_SERIAL); 45 | dispatch_queue_t dQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); 46 | dispatch_set_target_queue(_queue, dQueue); 47 | } 48 | return self; 49 | } 50 | 51 | - (NSString *)randomBitString { 52 | u_int8_t length = 10; 53 | char data[length]; 54 | for (int x = 0; x< length; data[x++] = (char)('A' + (arc4random_uniform(26)))); 55 | return [[NSString alloc] initWithBytes:data length:length encoding:NSUTF8StringEncoding]; 56 | } 57 | 58 | #pragma mark - sync 59 | + (void)execSyncBlock:(void (^)())block { 60 | [[HJSynchronizeSerial synchronizeQueue] execSyncBlock:block]; 61 | } 62 | 63 | - (void)execSyncBlock:(void (^)())block { 64 | dispatch_sync(_queue, ^{ 65 | block(); 66 | }); 67 | } 68 | 69 | #pragma mark - asyn 70 | + (void)execAsynBlock:(void (^)())block { 71 | [[HJSynchronizeSerial synchronizeQueue] execAsynBlock:block]; 72 | } 73 | 74 | - (void)execAsynBlock:(void (^)())block { 75 | dispatch_async(_queue, ^{ 76 | block(); 77 | }); 78 | } 79 | 80 | @end 81 | -------------------------------------------------------------------------------- /HJSynchronizeDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | A0745A941B4DFCA500C4F585 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = A0745A931B4DFCA500C4F585 /* main.m */; }; 11 | A0745A971B4DFCA500C4F585 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = A0745A961B4DFCA500C4F585 /* AppDelegate.m */; }; 12 | A0745A9A1B4DFCA500C4F585 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A0745A991B4DFCA500C4F585 /* ViewController.m */; }; 13 | A0745A9D1B4DFCA500C4F585 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A0745A9B1B4DFCA500C4F585 /* Main.storyboard */; }; 14 | A0745A9F1B4DFCA500C4F585 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A0745A9E1B4DFCA500C4F585 /* Images.xcassets */; }; 15 | A0745AA21B4DFCA500C4F585 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = A0745AA01B4DFCA500C4F585 /* LaunchScreen.xib */; }; 16 | A0745AAE1B4DFCA500C4F585 /* HJSynchronizeDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A0745AAD1B4DFCA500C4F585 /* HJSynchronizeDemoTests.m */; }; 17 | A0745ABA1B4DFCFB00C4F585 /* HJSynchronizeQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = A0745AB91B4DFCFB00C4F585 /* HJSynchronizeQueue.m */; }; 18 | A0F0C9681C1AB74300A59A76 /* HJSynchronizeSerial.m in Sources */ = {isa = PBXBuildFile; fileRef = A0F0C9671C1AB74300A59A76 /* HJSynchronizeSerial.m */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXContainerItemProxy section */ 22 | A0745AA81B4DFCA500C4F585 /* PBXContainerItemProxy */ = { 23 | isa = PBXContainerItemProxy; 24 | containerPortal = A0745A861B4DFCA500C4F585 /* Project object */; 25 | proxyType = 1; 26 | remoteGlobalIDString = A0745A8D1B4DFCA500C4F585; 27 | remoteInfo = HJSynchronizeDemo; 28 | }; 29 | /* End PBXContainerItemProxy section */ 30 | 31 | /* Begin PBXFileReference section */ 32 | A0166A671B4E96280015F1A1 /* HJSynchronize.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HJSynchronize.h; sourceTree = ""; }; 33 | A0745A8E1B4DFCA500C4F585 /* HJSynchronizeDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HJSynchronizeDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 34 | A0745A921B4DFCA500C4F585 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 35 | A0745A931B4DFCA500C4F585 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 36 | A0745A951B4DFCA500C4F585 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 37 | A0745A961B4DFCA500C4F585 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 38 | A0745A981B4DFCA500C4F585 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 39 | A0745A991B4DFCA500C4F585 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 40 | A0745A9C1B4DFCA500C4F585 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 41 | A0745A9E1B4DFCA500C4F585 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 42 | A0745AA11B4DFCA500C4F585 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 43 | A0745AA71B4DFCA500C4F585 /* HJSynchronizeDemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = HJSynchronizeDemoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 44 | A0745AAC1B4DFCA500C4F585 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 45 | A0745AAD1B4DFCA500C4F585 /* HJSynchronizeDemoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HJSynchronizeDemoTests.m; sourceTree = ""; }; 46 | A0745AB81B4DFCFB00C4F585 /* HJSynchronizeQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HJSynchronizeQueue.h; sourceTree = ""; }; 47 | A0745AB91B4DFCFB00C4F585 /* HJSynchronizeQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HJSynchronizeQueue.m; sourceTree = ""; }; 48 | A0F0C9661C1AB74300A59A76 /* HJSynchronizeSerial.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HJSynchronizeSerial.h; sourceTree = ""; }; 49 | A0F0C9671C1AB74300A59A76 /* HJSynchronizeSerial.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HJSynchronizeSerial.m; sourceTree = ""; }; 50 | /* End PBXFileReference section */ 51 | 52 | /* Begin PBXFrameworksBuildPhase section */ 53 | A0745A8B1B4DFCA500C4F585 /* Frameworks */ = { 54 | isa = PBXFrameworksBuildPhase; 55 | buildActionMask = 2147483647; 56 | files = ( 57 | ); 58 | runOnlyForDeploymentPostprocessing = 0; 59 | }; 60 | A0745AA41B4DFCA500C4F585 /* Frameworks */ = { 61 | isa = PBXFrameworksBuildPhase; 62 | buildActionMask = 2147483647; 63 | files = ( 64 | ); 65 | runOnlyForDeploymentPostprocessing = 0; 66 | }; 67 | /* End PBXFrameworksBuildPhase section */ 68 | 69 | /* Begin PBXGroup section */ 70 | A0745A851B4DFCA500C4F585 = { 71 | isa = PBXGroup; 72 | children = ( 73 | A0745A901B4DFCA500C4F585 /* HJSynchronizeDemo */, 74 | A0745AAA1B4DFCA500C4F585 /* HJSynchronizeDemoTests */, 75 | A0745A8F1B4DFCA500C4F585 /* Products */, 76 | ); 77 | sourceTree = ""; 78 | }; 79 | A0745A8F1B4DFCA500C4F585 /* Products */ = { 80 | isa = PBXGroup; 81 | children = ( 82 | A0745A8E1B4DFCA500C4F585 /* HJSynchronizeDemo.app */, 83 | A0745AA71B4DFCA500C4F585 /* HJSynchronizeDemoTests.xctest */, 84 | ); 85 | name = Products; 86 | sourceTree = ""; 87 | }; 88 | A0745A901B4DFCA500C4F585 /* HJSynchronizeDemo */ = { 89 | isa = PBXGroup; 90 | children = ( 91 | A0745AB71B4DFCD900C4F585 /* HJSynchronize */, 92 | A0745A951B4DFCA500C4F585 /* AppDelegate.h */, 93 | A0745A961B4DFCA500C4F585 /* AppDelegate.m */, 94 | A0745A981B4DFCA500C4F585 /* ViewController.h */, 95 | A0745A991B4DFCA500C4F585 /* ViewController.m */, 96 | A0745A9B1B4DFCA500C4F585 /* Main.storyboard */, 97 | A0745A9E1B4DFCA500C4F585 /* Images.xcassets */, 98 | A0745AA01B4DFCA500C4F585 /* LaunchScreen.xib */, 99 | A0745A911B4DFCA500C4F585 /* Supporting Files */, 100 | ); 101 | path = HJSynchronizeDemo; 102 | sourceTree = ""; 103 | }; 104 | A0745A911B4DFCA500C4F585 /* Supporting Files */ = { 105 | isa = PBXGroup; 106 | children = ( 107 | A0745A921B4DFCA500C4F585 /* Info.plist */, 108 | A0745A931B4DFCA500C4F585 /* main.m */, 109 | ); 110 | name = "Supporting Files"; 111 | sourceTree = ""; 112 | }; 113 | A0745AAA1B4DFCA500C4F585 /* HJSynchronizeDemoTests */ = { 114 | isa = PBXGroup; 115 | children = ( 116 | A0745AAD1B4DFCA500C4F585 /* HJSynchronizeDemoTests.m */, 117 | A0745AAB1B4DFCA500C4F585 /* Supporting Files */, 118 | ); 119 | path = HJSynchronizeDemoTests; 120 | sourceTree = ""; 121 | }; 122 | A0745AAB1B4DFCA500C4F585 /* Supporting Files */ = { 123 | isa = PBXGroup; 124 | children = ( 125 | A0745AAC1B4DFCA500C4F585 /* Info.plist */, 126 | ); 127 | name = "Supporting Files"; 128 | sourceTree = ""; 129 | }; 130 | A0745AB71B4DFCD900C4F585 /* HJSynchronize */ = { 131 | isa = PBXGroup; 132 | children = ( 133 | A0166A671B4E96280015F1A1 /* HJSynchronize.h */, 134 | A0F0C9661C1AB74300A59A76 /* HJSynchronizeSerial.h */, 135 | A0F0C9671C1AB74300A59A76 /* HJSynchronizeSerial.m */, 136 | A0745AB81B4DFCFB00C4F585 /* HJSynchronizeQueue.h */, 137 | A0745AB91B4DFCFB00C4F585 /* HJSynchronizeQueue.m */, 138 | ); 139 | path = HJSynchronize; 140 | sourceTree = SOURCE_ROOT; 141 | }; 142 | /* End PBXGroup section */ 143 | 144 | /* Begin PBXNativeTarget section */ 145 | A0745A8D1B4DFCA500C4F585 /* HJSynchronizeDemo */ = { 146 | isa = PBXNativeTarget; 147 | buildConfigurationList = A0745AB11B4DFCA500C4F585 /* Build configuration list for PBXNativeTarget "HJSynchronizeDemo" */; 148 | buildPhases = ( 149 | A0745A8A1B4DFCA500C4F585 /* Sources */, 150 | A0745A8B1B4DFCA500C4F585 /* Frameworks */, 151 | A0745A8C1B4DFCA500C4F585 /* Resources */, 152 | ); 153 | buildRules = ( 154 | ); 155 | dependencies = ( 156 | ); 157 | name = HJSynchronizeDemo; 158 | productName = HJSynchronizeDemo; 159 | productReference = A0745A8E1B4DFCA500C4F585 /* HJSynchronizeDemo.app */; 160 | productType = "com.apple.product-type.application"; 161 | }; 162 | A0745AA61B4DFCA500C4F585 /* HJSynchronizeDemoTests */ = { 163 | isa = PBXNativeTarget; 164 | buildConfigurationList = A0745AB41B4DFCA500C4F585 /* Build configuration list for PBXNativeTarget "HJSynchronizeDemoTests" */; 165 | buildPhases = ( 166 | A0745AA31B4DFCA500C4F585 /* Sources */, 167 | A0745AA41B4DFCA500C4F585 /* Frameworks */, 168 | A0745AA51B4DFCA500C4F585 /* Resources */, 169 | ); 170 | buildRules = ( 171 | ); 172 | dependencies = ( 173 | A0745AA91B4DFCA500C4F585 /* PBXTargetDependency */, 174 | ); 175 | name = HJSynchronizeDemoTests; 176 | productName = HJSynchronizeDemoTests; 177 | productReference = A0745AA71B4DFCA500C4F585 /* HJSynchronizeDemoTests.xctest */; 178 | productType = "com.apple.product-type.bundle.unit-test"; 179 | }; 180 | /* End PBXNativeTarget section */ 181 | 182 | /* Begin PBXProject section */ 183 | A0745A861B4DFCA500C4F585 /* Project object */ = { 184 | isa = PBXProject; 185 | attributes = { 186 | LastUpgradeCheck = 0640; 187 | ORGANIZATIONNAME = olinone; 188 | TargetAttributes = { 189 | A0745A8D1B4DFCA500C4F585 = { 190 | CreatedOnToolsVersion = 6.4; 191 | }; 192 | A0745AA61B4DFCA500C4F585 = { 193 | CreatedOnToolsVersion = 6.4; 194 | TestTargetID = A0745A8D1B4DFCA500C4F585; 195 | }; 196 | }; 197 | }; 198 | buildConfigurationList = A0745A891B4DFCA500C4F585 /* Build configuration list for PBXProject "HJSynchronizeDemo" */; 199 | compatibilityVersion = "Xcode 3.2"; 200 | developmentRegion = English; 201 | hasScannedForEncodings = 0; 202 | knownRegions = ( 203 | en, 204 | Base, 205 | ); 206 | mainGroup = A0745A851B4DFCA500C4F585; 207 | productRefGroup = A0745A8F1B4DFCA500C4F585 /* Products */; 208 | projectDirPath = ""; 209 | projectRoot = ""; 210 | targets = ( 211 | A0745A8D1B4DFCA500C4F585 /* HJSynchronizeDemo */, 212 | A0745AA61B4DFCA500C4F585 /* HJSynchronizeDemoTests */, 213 | ); 214 | }; 215 | /* End PBXProject section */ 216 | 217 | /* Begin PBXResourcesBuildPhase section */ 218 | A0745A8C1B4DFCA500C4F585 /* Resources */ = { 219 | isa = PBXResourcesBuildPhase; 220 | buildActionMask = 2147483647; 221 | files = ( 222 | A0745A9D1B4DFCA500C4F585 /* Main.storyboard in Resources */, 223 | A0745AA21B4DFCA500C4F585 /* LaunchScreen.xib in Resources */, 224 | A0745A9F1B4DFCA500C4F585 /* Images.xcassets in Resources */, 225 | ); 226 | runOnlyForDeploymentPostprocessing = 0; 227 | }; 228 | A0745AA51B4DFCA500C4F585 /* Resources */ = { 229 | isa = PBXResourcesBuildPhase; 230 | buildActionMask = 2147483647; 231 | files = ( 232 | ); 233 | runOnlyForDeploymentPostprocessing = 0; 234 | }; 235 | /* End PBXResourcesBuildPhase section */ 236 | 237 | /* Begin PBXSourcesBuildPhase section */ 238 | A0745A8A1B4DFCA500C4F585 /* Sources */ = { 239 | isa = PBXSourcesBuildPhase; 240 | buildActionMask = 2147483647; 241 | files = ( 242 | A0F0C9681C1AB74300A59A76 /* HJSynchronizeSerial.m in Sources */, 243 | A0745A9A1B4DFCA500C4F585 /* ViewController.m in Sources */, 244 | A0745A971B4DFCA500C4F585 /* AppDelegate.m in Sources */, 245 | A0745ABA1B4DFCFB00C4F585 /* HJSynchronizeQueue.m in Sources */, 246 | A0745A941B4DFCA500C4F585 /* main.m in Sources */, 247 | ); 248 | runOnlyForDeploymentPostprocessing = 0; 249 | }; 250 | A0745AA31B4DFCA500C4F585 /* Sources */ = { 251 | isa = PBXSourcesBuildPhase; 252 | buildActionMask = 2147483647; 253 | files = ( 254 | A0745AAE1B4DFCA500C4F585 /* HJSynchronizeDemoTests.m in Sources */, 255 | ); 256 | runOnlyForDeploymentPostprocessing = 0; 257 | }; 258 | /* End PBXSourcesBuildPhase section */ 259 | 260 | /* Begin PBXTargetDependency section */ 261 | A0745AA91B4DFCA500C4F585 /* PBXTargetDependency */ = { 262 | isa = PBXTargetDependency; 263 | target = A0745A8D1B4DFCA500C4F585 /* HJSynchronizeDemo */; 264 | targetProxy = A0745AA81B4DFCA500C4F585 /* PBXContainerItemProxy */; 265 | }; 266 | /* End PBXTargetDependency section */ 267 | 268 | /* Begin PBXVariantGroup section */ 269 | A0745A9B1B4DFCA500C4F585 /* Main.storyboard */ = { 270 | isa = PBXVariantGroup; 271 | children = ( 272 | A0745A9C1B4DFCA500C4F585 /* Base */, 273 | ); 274 | name = Main.storyboard; 275 | sourceTree = ""; 276 | }; 277 | A0745AA01B4DFCA500C4F585 /* LaunchScreen.xib */ = { 278 | isa = PBXVariantGroup; 279 | children = ( 280 | A0745AA11B4DFCA500C4F585 /* Base */, 281 | ); 282 | name = LaunchScreen.xib; 283 | sourceTree = ""; 284 | }; 285 | /* End PBXVariantGroup section */ 286 | 287 | /* Begin XCBuildConfiguration section */ 288 | A0745AAF1B4DFCA500C4F585 /* Debug */ = { 289 | isa = XCBuildConfiguration; 290 | buildSettings = { 291 | ALWAYS_SEARCH_USER_PATHS = NO; 292 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 293 | CLANG_CXX_LIBRARY = "libc++"; 294 | CLANG_ENABLE_MODULES = YES; 295 | CLANG_ENABLE_OBJC_ARC = YES; 296 | CLANG_WARN_BOOL_CONVERSION = YES; 297 | CLANG_WARN_CONSTANT_CONVERSION = YES; 298 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 299 | CLANG_WARN_EMPTY_BODY = YES; 300 | CLANG_WARN_ENUM_CONVERSION = YES; 301 | CLANG_WARN_INT_CONVERSION = YES; 302 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 303 | CLANG_WARN_UNREACHABLE_CODE = YES; 304 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 305 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 306 | COPY_PHASE_STRIP = NO; 307 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 308 | ENABLE_STRICT_OBJC_MSGSEND = YES; 309 | GCC_C_LANGUAGE_STANDARD = gnu99; 310 | GCC_DYNAMIC_NO_PIC = NO; 311 | GCC_NO_COMMON_BLOCKS = YES; 312 | GCC_OPTIMIZATION_LEVEL = 0; 313 | GCC_PREPROCESSOR_DEFINITIONS = ( 314 | "DEBUG=1", 315 | "$(inherited)", 316 | ); 317 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 318 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 319 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 320 | GCC_WARN_UNDECLARED_SELECTOR = YES; 321 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 322 | GCC_WARN_UNUSED_FUNCTION = YES; 323 | GCC_WARN_UNUSED_VARIABLE = YES; 324 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 325 | MTL_ENABLE_DEBUG_INFO = YES; 326 | ONLY_ACTIVE_ARCH = YES; 327 | SDKROOT = iphoneos; 328 | }; 329 | name = Debug; 330 | }; 331 | A0745AB01B4DFCA500C4F585 /* Release */ = { 332 | isa = XCBuildConfiguration; 333 | buildSettings = { 334 | ALWAYS_SEARCH_USER_PATHS = NO; 335 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 336 | CLANG_CXX_LIBRARY = "libc++"; 337 | CLANG_ENABLE_MODULES = YES; 338 | CLANG_ENABLE_OBJC_ARC = YES; 339 | CLANG_WARN_BOOL_CONVERSION = YES; 340 | CLANG_WARN_CONSTANT_CONVERSION = YES; 341 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 342 | CLANG_WARN_EMPTY_BODY = YES; 343 | CLANG_WARN_ENUM_CONVERSION = YES; 344 | CLANG_WARN_INT_CONVERSION = YES; 345 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 346 | CLANG_WARN_UNREACHABLE_CODE = YES; 347 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 348 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 349 | COPY_PHASE_STRIP = NO; 350 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 351 | ENABLE_NS_ASSERTIONS = NO; 352 | ENABLE_STRICT_OBJC_MSGSEND = YES; 353 | GCC_C_LANGUAGE_STANDARD = gnu99; 354 | GCC_NO_COMMON_BLOCKS = YES; 355 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 356 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 357 | GCC_WARN_UNDECLARED_SELECTOR = YES; 358 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 359 | GCC_WARN_UNUSED_FUNCTION = YES; 360 | GCC_WARN_UNUSED_VARIABLE = YES; 361 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 362 | MTL_ENABLE_DEBUG_INFO = NO; 363 | SDKROOT = iphoneos; 364 | VALIDATE_PRODUCT = YES; 365 | }; 366 | name = Release; 367 | }; 368 | A0745AB21B4DFCA500C4F585 /* Debug */ = { 369 | isa = XCBuildConfiguration; 370 | buildSettings = { 371 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 372 | INFOPLIST_FILE = HJSynchronizeDemo/Info.plist; 373 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 374 | PRODUCT_NAME = "$(TARGET_NAME)"; 375 | }; 376 | name = Debug; 377 | }; 378 | A0745AB31B4DFCA500C4F585 /* Release */ = { 379 | isa = XCBuildConfiguration; 380 | buildSettings = { 381 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 382 | INFOPLIST_FILE = HJSynchronizeDemo/Info.plist; 383 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 384 | PRODUCT_NAME = "$(TARGET_NAME)"; 385 | }; 386 | name = Release; 387 | }; 388 | A0745AB51B4DFCA500C4F585 /* Debug */ = { 389 | isa = XCBuildConfiguration; 390 | buildSettings = { 391 | BUNDLE_LOADER = "$(TEST_HOST)"; 392 | FRAMEWORK_SEARCH_PATHS = ( 393 | "$(SDKROOT)/Developer/Library/Frameworks", 394 | "$(inherited)", 395 | ); 396 | GCC_PREPROCESSOR_DEFINITIONS = ( 397 | "DEBUG=1", 398 | "$(inherited)", 399 | ); 400 | INFOPLIST_FILE = HJSynchronizeDemoTests/Info.plist; 401 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 402 | PRODUCT_NAME = "$(TARGET_NAME)"; 403 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/HJSynchronizeDemo.app/HJSynchronizeDemo"; 404 | }; 405 | name = Debug; 406 | }; 407 | A0745AB61B4DFCA500C4F585 /* Release */ = { 408 | isa = XCBuildConfiguration; 409 | buildSettings = { 410 | BUNDLE_LOADER = "$(TEST_HOST)"; 411 | FRAMEWORK_SEARCH_PATHS = ( 412 | "$(SDKROOT)/Developer/Library/Frameworks", 413 | "$(inherited)", 414 | ); 415 | INFOPLIST_FILE = HJSynchronizeDemoTests/Info.plist; 416 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 417 | PRODUCT_NAME = "$(TARGET_NAME)"; 418 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/HJSynchronizeDemo.app/HJSynchronizeDemo"; 419 | }; 420 | name = Release; 421 | }; 422 | /* End XCBuildConfiguration section */ 423 | 424 | /* Begin XCConfigurationList section */ 425 | A0745A891B4DFCA500C4F585 /* Build configuration list for PBXProject "HJSynchronizeDemo" */ = { 426 | isa = XCConfigurationList; 427 | buildConfigurations = ( 428 | A0745AAF1B4DFCA500C4F585 /* Debug */, 429 | A0745AB01B4DFCA500C4F585 /* Release */, 430 | ); 431 | defaultConfigurationIsVisible = 0; 432 | defaultConfigurationName = Release; 433 | }; 434 | A0745AB11B4DFCA500C4F585 /* Build configuration list for PBXNativeTarget "HJSynchronizeDemo" */ = { 435 | isa = XCConfigurationList; 436 | buildConfigurations = ( 437 | A0745AB21B4DFCA500C4F585 /* Debug */, 438 | A0745AB31B4DFCA500C4F585 /* Release */, 439 | ); 440 | defaultConfigurationIsVisible = 0; 441 | defaultConfigurationName = Release; 442 | }; 443 | A0745AB41B4DFCA500C4F585 /* Build configuration list for PBXNativeTarget "HJSynchronizeDemoTests" */ = { 444 | isa = XCConfigurationList; 445 | buildConfigurations = ( 446 | A0745AB51B4DFCA500C4F585 /* Debug */, 447 | A0745AB61B4DFCA500C4F585 /* Release */, 448 | ); 449 | defaultConfigurationIsVisible = 0; 450 | defaultConfigurationName = Release; 451 | }; 452 | /* End XCConfigurationList section */ 453 | }; 454 | rootObject = A0745A861B4DFCA500C4F585 /* Project object */; 455 | } 456 | -------------------------------------------------------------------------------- /HJSynchronizeDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /HJSynchronizeDemo.xcodeproj/project.xcworkspace/xcuserdata/haijiao.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/panghaijiao/HJSynchronizeDemo/e6a2e3e84ed51352b7ff65f8003b66175e2d8839/HJSynchronizeDemo.xcodeproj/project.xcworkspace/xcuserdata/haijiao.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /HJSynchronizeDemo.xcodeproj/xcuserdata/haijiao.xcuserdatad/xcschemes/HJSynchronizeDemo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 29 | 35 | 36 | 37 | 38 | 39 | 44 | 45 | 47 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 65 | 66 | 75 | 77 | 83 | 84 | 85 | 86 | 87 | 88 | 94 | 96 | 102 | 103 | 104 | 105 | 107 | 108 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /HJSynchronizeDemo.xcodeproj/xcuserdata/haijiao.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | HJSynchronizeDemo.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | A0745A8D1B4DFCA500C4F585 16 | 17 | primary 18 | 19 | 20 | A0745AA61B4DFCA500C4F585 21 | 22 | primary 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /HJSynchronizeDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // HJSynchronizeDemo 4 | // 5 | // Created by haijiao on 15/7/9. 6 | // Copyright (c) 2015年 olinone. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /HJSynchronizeDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // HJSynchronizeDemo 4 | // 5 | // Created by haijiao on 15/7/9. 6 | // Copyright (c) 2015年 olinone. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 25 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /HJSynchronizeDemo/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /HJSynchronizeDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /HJSynchronizeDemo/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /HJSynchronizeDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.olinone.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /HJSynchronizeDemo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // HJSynchronizeDemo 4 | // 5 | // Created by haijiao on 15/7/9. 6 | // Copyright (c) 2015年 olinone. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /HJSynchronizeDemo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // HJSynchronizeDemo 4 | // 5 | // Created by haijiao on 15/7/9. 6 | // Copyright (c) 2015年 olinone. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "HJSynchronize.h" 11 | 12 | @interface ViewController () 13 | 14 | @property (nonatomic, strong) NSArray *testGCDArrays; 15 | @property (nonatomic, strong) NSArray *testQueueArrays; 16 | 17 | @end 18 | 19 | @implementation ViewController 20 | 21 | - (void)viewDidLoad { 22 | [super viewDidLoad]; 23 | self.testGCDArrays = @[@"串行同步", @"并行同步"]; 24 | self.testQueueArrays = @[@"串行同步", @"并行同步", @"并行同步取消其它任务"]; 25 | } 26 | 27 | - (void)test_multiTask { 28 | NSLog(@"\n\n 多线程 \n"); 29 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 30 | [self execTaskA]; 31 | }); 32 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 33 | [self execTaskB]; 34 | }); 35 | } 36 | 37 | #pragma mark - GCD 38 | - (void)test_gcd_serialSyn { 39 | NSLog(@"\n\n GCD 串行执行,堵塞线程 \n"); 40 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 41 | [HJSynchronizeSerial execSyncBlock:^{ 42 | [self execTaskA]; 43 | NSLog(@"A Finish"); 44 | }]; 45 | NSLog(@"A GOON EXEC"); 46 | }); 47 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 48 | [HJSynchronizeSerial execSyncBlock:^{ 49 | [self execTaskB]; 50 | NSLog(@"B Finish"); 51 | }]; 52 | NSLog(@"B GOON EXEC"); 53 | }); 54 | } 55 | 56 | - (void)test_gcd_concurrentSyn { 57 | NSLog(@"\n\n GCD 并行执行,不堵塞线程 \n"); 58 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 59 | [HJSynchronizeSerial execAsynBlock:^{ 60 | [self execTaskA]; 61 | NSLog(@"A Finish"); 62 | }]; 63 | NSLog(@"A GOON EXEC"); 64 | }); 65 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 66 | [HJSynchronizeSerial execAsynBlock:^{ 67 | [self execTaskB]; 68 | NSLog(@"B Finish"); 69 | }]; 70 | NSLog(@"B GOON EXEC"); 71 | }); 72 | } 73 | 74 | #pragma mark - Queue 75 | - (void)test_serialSyn { 76 | NSLog(@"\n\n Queue 串行执行,堵塞线程 \n"); 77 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 78 | [HJSynchronizeQueue execSyncBlock:^{ 79 | [self execTaskA]; 80 | NSLog(@"A Finish"); 81 | }]; 82 | NSLog(@"A GOON EXEC"); 83 | }); 84 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 85 | [HJSynchronizeQueue execSyncBlock:^{ 86 | [self execTaskB]; 87 | NSLog(@"B Finish"); 88 | }]; 89 | NSLog(@"B GOON EXEC"); 90 | }); 91 | } 92 | 93 | - (void)test_concurrentSyn { 94 | NSLog(@"\n\n Queue 并行执行,不堵塞线程 \n"); 95 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 96 | [HJSynchronizeQueue execAsynBlock:^{ 97 | [self execTaskA]; 98 | NSLog(@"A Finish"); 99 | }]; 100 | NSLog(@"A GOON EXEC"); 101 | }); 102 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 103 | [HJSynchronizeQueue execAsynBlock:^{ 104 | [self execTaskB]; 105 | NSLog(@"B Finish"); 106 | }]; 107 | NSLog(@"B GOON EXEC"); 108 | }); 109 | } 110 | 111 | - (void)test_concurrentSynCancel { 112 | NSLog(@"\n\n Queue 并行执行的时候可以取消其它任务 \n"); 113 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 114 | [HJSynchronizeQueue execAsynBlock:^{ 115 | NSLog(@"B Cancel"); 116 | [HJSynchronizeQueue cancelAllOperations]; 117 | [self execTaskA]; 118 | NSLog(@"A Finish"); 119 | }]; 120 | }); 121 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 122 | [HJSynchronizeQueue execAsynBlock:^{ 123 | NSLog(@"A Cancel"); 124 | [HJSynchronizeQueue cancelAllOperations]; 125 | [self execTaskB]; 126 | NSLog(@"B Finish"); 127 | }]; 128 | }); 129 | } 130 | 131 | #pragma mark - 132 | - (void)execTaskA { 133 | for (u_int8_t i = 0; i < 5; i++) { 134 | NSLog(@"A %d", i); 135 | } 136 | } 137 | 138 | - (void)execTaskB { 139 | for (u_int8_t i = 0; i < 5; i++) { 140 | NSLog(@"B %d", i); 141 | } 142 | } 143 | 144 | #pragma mark - 145 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 146 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; 147 | if (indexPath.section == 0) { 148 | [self test_multiTask]; 149 | return; 150 | } 151 | if (indexPath.section == 0) { 152 | switch (indexPath.row) { 153 | case 0: 154 | [self test_gcd_serialSyn]; 155 | break; 156 | case 1: 157 | [self test_gcd_concurrentSyn]; 158 | break; 159 | default: 160 | break; 161 | } 162 | return; 163 | } 164 | switch (indexPath.row) { 165 | case 0: 166 | [self test_serialSyn]; 167 | break; 168 | case 1: 169 | [self test_concurrentSyn]; 170 | break; 171 | case 2: 172 | [self test_concurrentSynCancel]; 173 | default: 174 | break; 175 | } 176 | } 177 | 178 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 179 | return 3; 180 | } 181 | 182 | - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 183 | switch (section) { 184 | case 0:return @""; 185 | case 1:return @"GCD"; 186 | case 2:return @"Queue"; 187 | default:return nil; 188 | } 189 | } 190 | 191 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 192 | switch (section) { 193 | case 0:return 1; 194 | case 1:return self.testGCDArrays.count; 195 | case 2:return self.testQueueArrays.count; 196 | default:return 0; 197 | } 198 | } 199 | 200 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 201 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 202 | if (!cell) { 203 | cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 204 | } 205 | switch (indexPath.section) { 206 | case 0: 207 | cell.textLabel.text = @"多线程"; 208 | break; 209 | case 1: 210 | cell.textLabel.text = self.testGCDArrays[indexPath.row]; 211 | break; 212 | case 2: 213 | cell.textLabel.text = self.testQueueArrays[indexPath.row]; 214 | break; 215 | } 216 | 217 | return cell; 218 | } 219 | 220 | @end 221 | -------------------------------------------------------------------------------- /HJSynchronizeDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // HJSynchronizeDemo 4 | // 5 | // Created by haijiao on 15/7/9. 6 | // Copyright (c) 2015年 olinone. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /HJSynchronizeDemoTests/HJSynchronizeDemoTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // HJSynchronizeDemoTests.m 3 | // HJSynchronizeDemoTests 4 | // 5 | // Created by haijiao on 15/7/9. 6 | // Copyright (c) 2015年 olinone. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface HJSynchronizeDemoTests : XCTestCase 13 | 14 | @end 15 | 16 | @implementation HJSynchronizeDemoTests 17 | 18 | - (void)setUp { 19 | [super setUp]; 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | } 22 | 23 | - (void)tearDown { 24 | // Put teardown code here. This method is called after the invocation of each test method in the class. 25 | [super tearDown]; 26 | } 27 | 28 | - (void)testExample { 29 | // This is an example of a functional test case. 30 | XCTAssert(YES, @"Pass"); 31 | } 32 | 33 | - (void)testPerformanceExample { 34 | // This is an example of a performance test case. 35 | [self measureBlock:^{ 36 | // Put the code you want to measure the time of here. 37 | }]; 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /HJSynchronizeDemoTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.olinone.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 olinone 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HJSynchronize 2 | 3 | This HJSynchronize is a multi thread synchronization tool used on iOS 4 | 5 | ## Example Usage 6 | 7 | Check out this [project](https://github.com/panghaijiao/HJSynchronizeDemo). It contains a few demos of the API in use for various scenarios. 8 | 9 | **Usage** 10 | 11 | *Asynchronous queue* 12 | 13 | ``` 14 | [HJSynchronizeQueue execAsynBlock:^{ 15 | // to do 16 | }]; 17 | ``` 18 | *Synchronization queue* 19 | 20 | ``` 21 | [HJSynchronizeQueue execAsynBlock:^{ 22 | // to do 23 | }]; 24 | ``` 25 | For more information you can visit the [web site](http://www.olinone.com/?p=250) 26 | 27 | ## Installation with CocoaPods 28 | 29 | [CocoaPods](http://cocoapods.org/) is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries in your projects. See the [Get Started](http://cocoapods.org/#get_started) section for more details. 30 | 31 | ## Podfile 32 | 33 | ``` 34 | pod 'HJSynchronize', :git => 'https://github.com/panghaijiao/HJSynchronize.git' 35 | ``` 36 | 37 | ## License 38 | 39 | [MIT license](http://www.opensource.org/licenses/mit-license.php). 40 | --------------------------------------------------------------------------------