//
Viewcontroller.m
ui-no-25
//
Created by Bruce on 15/8/27.
Copyright (c) 2015 Bruce. All rights reserved.
//
#import "ViewController.h"
@interface Viewcontroller ()
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
You cannot use operation directly because there is no way to manipulate it directly, just a base class (abstract class)
Nsinvocationoperation *invocation = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (act:) object:@ "Invocation"];
If you use operation alone you must start manually
[Invocation start];
Operation default on current thread execution (synchronous execution)
Nsinvocationoperation *invocation1 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (act:) object:@ "Invocation1"];
If you use operation alone you must start manually
Nsinvocationoperation *invocation2 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (act:) object:@ "Invocation2"];
Nsinvocationoperation *invocation3 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (act:) object:@ "Invocation3"];
Nsinvocationoperation *invocation4 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (act:) object:@ "Invocation4"];
Nsoperationqueue do not manually start the thread so comment out start
[Invocation1 start];
Operation default on current thread execution (synchronous execution)
Nsblockoperation *blockoperation = [Nsblockoperation blockoperationwithblock:^{
Specific methods of implementation
[Self act:@ "blockoperation"];
}];
Add action
Nsblockoperation the number of operands is greater than 1 will open thread execution
[Blockoperation addexecutionblock:^{
[Self act:@ "blockOperation2"];
}];
[Blockoperation start];
Operation default on current thread execution (synchronous execution)
Nsoperationqueue (thread queue) management thread (default will open thread to execute)
Nsoperationqueue do not manually start the thread
Asynchronous execution
Nsoperationqueue *queue = [[Nsoperationqueue alloc]init];
Sets the maximum number of concurrent threads in a queue (can allow several operations to execute concurrently)
Queue.maxconcurrentoperationcount = 1;
[Queue addoperation:invocation];
[Queue Addoperation:invocation1];
[Queue Addoperation:invocation2];
[Queue Addoperation:invocation3];
[Queue Addoperation:invocation4];
[Queue addoperation:blockoperation];
}
-(void) Act: (ID) sender
{
NSLog (@ "%@\n%@", [Nsthread Currentthread],sender);
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
@end
Operation (Custom multithreading)