#pragma mark - Override-(BOOL)isConcurrent{returnYES;}-(BOOL)isExecuting{returnself.state==MyOperationExecutingState;}-(BOOL)isFinished{returnself.state==MyOperationFinishedState;}-(void)start{// 在這邊坐些什麼吧}
-(void)start{if([selfisReady]){self.state=MyOperationExecutingState;[selfperformSelector:@selector(operationDidStart)onThread:[[selfclass]threadForMyOperation]withObject:nilwaitUntilDone:NO];}}-(void)operationDidStart{if(self.isCancelled){self.state=MyOperationFinishedState;}else{NSLog(@"Operation is running %@ thread",[NSThreadcurrentThread]);self.action();self.state=MyOperationFinishedState;}}
-(void)runOperations{MyOperation*myOperation=[[MyOperationalloc]initWithAction:^{NSLog(@"this is MyOperation");}];NSBlockOperation*blockOperation=[[NSBlockOperationalloc]init];[blockOperationaddExecutionBlock:^{NSLog(@"this is block Operation");}];[myOperationstart];[blockOperationstart];}
-(void)cancel{[selfwillChangeValueForKey:@"isCancelled"];_cancelled=YES;[selfdidChangeValueForKey:@"isCancelled"];}-(void)start{if([selfisReady]){[selfwillChangeValueForKey:@"isExecuting"];[selfwillChangeValueForKey:@"isReady"];_state=MyOperationExecutingState;[selfdidChangeValueForKey:@"isReady"];[selfdidChangeValueForKey:@"isExecuting"];[selfperformSelector:@selector(operationDidStart)onThread:[[selfclass]threadForMyOperation]withObject:nilwaitUntilDone:NO];}}-(void)operationDidStart{if(self.isCancelled){[selfwillChangeValueForKey:@"isFinished"];[selfwillChangeValueForKey:@"isCancelled"];_state=MyOperationFinishedState;[selfdidChangeValueForKey:@"isCancelled"];[selfdidChangeValueForKey:@"isFinished"];}else{NSLog(@"Operation is running %@ thread",[NSThreadcurrentThread]);self.action();[selfwillChangeValueForKey:@"isFinished"];[selfwillChangeValueForKey:@"isExecuting"];_state=MyOperationFinishedState;[selfdidChangeValueForKey:@"isExecuting"];[selfdidChangeValueForKey:@"isFinished"];}}
-(void)runOperationsatCustomQueue{NSBlockOperation*blockOperation01=[[NSBlockOperationalloc]init];[blockOperation01addExecutionBlock:^{NSLog(@"%@",[NSThreadcurrentThread]);NSLog(@"this is no.1 block Operation");}];NSBlockOperation*blockOperation02=[[NSBlockOperationalloc]init];[blockOperation02addExecutionBlock:^{NSLog(@"%@",[NSThreadcurrentThread]);NSLog(@"this is no.2 block Operation, start sleep 1 second");sleep(1);NSLog(@"no.2 wake up.");}];NSBlockOperation*blockOperation03=[[NSBlockOperationalloc]init];[blockOperation03addExecutionBlock:^{NSLog(@"%@",[NSThreadcurrentThread]);NSLog(@"this is no.3 block Operation, no.2 operation is completed, start task");}];[blockOperation03addDependency:blockOperation02];NSOperationQueue*queue=[[NSOperationQueuealloc]init];[queuesetSuspended:YES];[queuesetMaxConcurrentOperationCount:5];[queueaddOperation:blockOperation01];[queueaddOperation:blockOperation02];[queueaddOperation:blockOperation03];[queuesetSuspended:NO];}
MyOperation*myOperation01=[[MyOperationalloc]initWithAction:^{NSLog(@"this is 01 MyOperation");}];MyOperation*myOperation02=[[MyOperationalloc]initWithAction:^{NSLog(@"this is 02 MyOperation");}];NSOperationQueue*queue=[[NSOperationQueuealloc]init];[queuesetSuspended:YES];[queuesetMaxConcurrentOperationCount:5];[queueaddOperation:myOperation01];[queueaddOperation:myOperation02];[queueaddOperation:blockOperation01];...