這裏主要使用到了dispatch的一些方法
直接拖控件,使用button和image。
重點
1.線程的類型是dispatch_queue_t
2.獲得主隊列的方法是dispatch_get_main_queue();
3.向隊列中添加方法dispatch_async(queue,^{ });
4.dispatch_queue_create(“隊列的名字”,同步/異步(DISPATCH_QUEUE_SERIAL))來創建隊列。
5.輸出當前隊列[NSThread currentThread]
6.自己創建的隊列要進行釋放
7.獲取系統隊列dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEAULT,0)
這次老師講的不詳細,要自己補充知識了
#import "ViewController.h"
@interface ViewController ()
- (IBAction)serial:(UIButton *)sender;
- (IBAction)showImage:(UIButton *)sender;
@property (retain, nonatomic) IBOutlet UIImageView *imgView;
- (IBAction)Parallels:(UIButton *)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark ------------GCD中的幾個概念
//1.Queue隊列 dispath queue 存放的是一個或多個對象(操作對象)
//2.Task 任務 具有一定功能的代碼段(函數,block)
//3.NSThread 爲了保證task的任務的順利執行,系統會開闢適當的thread(線程),在開闢的線程中,執行Task
//4.GCD Grand Central Dispatch
//5.Queue分爲兩種類型
//①.串行隊列(SerialQueue)
// 特點:執行的任務是先進先出(FIFO),排在隊列最前面的任務先執行,排在後面的後執行
//②並行隊列 (conCurrentQueue)
// 特點:先進先出(FIFO),一旦第一個任務開始執行,後面的任務也開始執行(無需等待前一個任務是否執行完成)
//獲取主隊列 -------
dispatch_get_main_queue();
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark ------------串行隊列
- (IBAction)serial:(UIButton *)sender {
//如果你想讓你的任務在串行隊列中執行,要使用SerialQueue
//如何爲獲取串行隊列(SerialQueue),有兩種途徑:
//第一種:使用系統自帶的mainQueue(主隊列)
//注意:這個mainQueue裏只有一個線程,而且是主線程
dispatch_queue_t queue = dispatch_get_main_queue();
//採用異步方式向隊列中添加任務
dispatch_async(queue, ^{
NSLog(@"任務一%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"任務二%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"任務三%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
//第二種:使用自己創建的隊列
//第一個參數表示隊列的名字(域名倒寫)
//第二個參數:決定當前這個方法創建出來的Queue是否是串行隊列
dispatch_queue_t queue1 = dispatch_queue_create("com.lanou4g.myQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue1, ^{
NSLog(@"任務五%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
dispatch_async(queue1, ^{
NSLog(@"任務四%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
//在MRC下,任何一個帶create創建的隊列,一定要記得釋放
dispatch_release(queue1);
}
- (IBAction)showImage:(UIButton *)sender {
__block ViewController *VC = self;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
//創建網址對象
NSString *string = @"http://image.zcool.com.cn/56/13/1308200901454.jpg";
NSURL * url = [NSURL URLWithString:string];
NSData * data = [NSData dataWithContentsOfURL:url];
UIImage * iamge = [UIImage imageWithData:data];
//獲取主線程,使用GCD的話,你要先獲得主隊列,因爲只有主線程中
//獲取主隊列
dispatch_queue_t queue1 = dispatch_get_main_queue();
dispatch_async(queue1, ^{
//當前是主線程的任務
VC.imgView.image = iamge;
NSLog(@"%@",[NSThread currentThread]);
});
});
}
#pragma mark --------------並行隊列
- (IBAction)Parallels:(UIButton *)sender {
//如果你想讓你的任務併發執行,我們使用conCurrenQueue
//獲取並行隊列,有兩種方式
//①使用系統提供的
// 第一種(系統提供的):全局隊列(globalQueue)
//第一個參數表示隊列的優先級(HIGH > DEFALUT > LOW >BACKGROND
//第二個參數是系統預留,現在沒有。
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 2);
//採用異步方式向隊列中添加代碼
// dispatch_async(queue, ^{
// NSLog(@"任務一%@,%d",[NSThread currentThread],[NSThread isMainThread]);
// });
// dispatch_async(queue, ^{
// NSLog(@"任務二%@,%d",[NSThread currentThread],[NSThread isMainThread]);
// });
// dispatch_async(queue, ^{
// NSLog(@"任務三%@,%d",[NSThread currentThread],[NSThread isMainThread]);
// });
//自己創建並行隊列
dispatch_queue_t queue2 = dispatch_queue_create("com.lanou4g.myGlobleQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue2, ^{
NSLog(@"任務一%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"任務二%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
dispatch_async(queue, ^{
NSLog(@"任務三%@,%d",[NSThread currentThread],[NSThread isMainThread]);
});
}
- (void)dealloc {
[_imgView release];
[super dealloc];
}
@end