iOS-多线程 ,整理集锦,多种线程的创建
生活随笔
收集整理的這篇文章主要介紹了
iOS-多线程 ,整理集锦,多种线程的创建
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2 {
3 self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
4
5 //創(chuàng)建線(xiàn)程的第一種方式
6 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"universe"];
7 [thread start];
8 [thread release];
9
10
11 //創(chuàng)建線(xiàn)程的第二種方式,NSThread類(lèi)方法
12 [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"yuzhou"];
13
14
15 //創(chuàng)建線(xiàn)程的第三種方法 NSObject方法
16 [self performSelectorInBackground:@selector(run:) withObject:@"nsobject thread"];
17
18 //創(chuàng)建線(xiàn)程的第四種方式
19 NSOperationQueue *oprationQueue = [[NSOperationQueue alloc] init];
20 [oprationQueue addOperationWithBlock:^{
21 //這個(gè)block語(yǔ)句塊在子線(xiàn)程中執(zhí)行
22 NSLog(@"oprationQueue");
23 }];
24 [oprationQueue release];
25
26 //第五種創(chuàng)建線(xiàn)程的方式
27 NSOperationQueue *oprationQueue1 = [[NSOperationQueue alloc] init];
28 oprationQueue1.maxConcurrentOperationCount = 1;//指定池子的并發(fā)數(shù)
29
30 //NSOperation 相當(dāng)于java中的runnable接口,繼承自它的就相當(dāng)一個(gè)任務(wù)
31 NSInvocationOperation *invation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run:) object:@"invation"];
32 [oprationQueue1 addOperation:invation];//將任務(wù)添加到池子里面,可以給池子添加多個(gè)任務(wù),并且指定它的并發(fā)數(shù)
33 [invation release];
34
35 //第二個(gè)任務(wù)
36 NSInvocationOperation *invation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run2:) object:@"invocation2"];
37 invation2.queuePriority = NSOperationQueuePriorityHigh;//設(shè)置線(xiàn)程優(yōu)先級(jí)
38 [oprationQueue1 addOperation:invation2];
39 [invation2 release];
40
41 [oprationQueue1 release];
42
43 //調(diào)用主線(xiàn)程,用來(lái)子線(xiàn)程和主線(xiàn)程交互,最后面的那個(gè)boolean參數(shù),如果為yes就是等這個(gè)方法執(zhí)行完了在執(zhí)行后面的代碼;如果為no的話(huà),就是不管這個(gè)方法執(zhí)行完了沒(méi)有,接著往下走
44 [self performSelectorOnMainThread:@selector(onMain) withObject:self waitUntilDone:YES];
45
46 //---------------------GCD----------------------支持多核,高效率的多線(xiàn)程技術(shù)
47 //創(chuàng)建多線(xiàn)程第六種方式
48 dispatch_queue_t queue = dispatch_queue_create("name", NULL);
49 //創(chuàng)建一個(gè)子線(xiàn)程
50 dispatch_async(queue, ^{
51 // 子線(xiàn)程code... ..
52 NSLog(@"GCD多線(xiàn)程");
53
54 //回到主線(xiàn)程
55 dispatch_sync(dispatch_get_main_queue(), ^{//其實(shí)這個(gè)也是在子線(xiàn)程中執(zhí)行的,只是把它放到了主線(xiàn)程的隊(duì)列中
56 Boolean isMain = [NSThread isMainThread];
57 if (isMain) {
58 NSLog(@"GCD主線(xiàn)程");
59 }
60 });
61 });
62
63
64 self.window.backgroundColor = [UIColor whiteColor];
65 [self.window makeKeyAndVisible];
66 return YES;
67 }
68
69 - (void)onMain
70 {
71 Boolean b = [NSThread isMainThread];
72 if (b) {
73 NSLog(@"onMain;;%d",b);
74 }
75 }
76
77 - (void) run:(NSString*)str
78 {
79 NSLog(@"多線(xiàn)程運(yùn)行:::%@",str);
80 }
81
82 - (void) run2:(NSString*)str
83 {
84 NSLog(@"多線(xiàn)程運(yùn)行:::%@",str);
85 }
86 這些都是基本的線(xiàn)程創(chuàng)建,用NSThread來(lái)進(jìn)行創(chuàng)建線(xiàn)程比較簡(jiǎn)單,如果是單一的創(chuàng)建線(xiàn)程可以用NSThread。直接創(chuàng)建可以使用。只需把線(xiàn)程執(zhí)行的函數(shù)和方法寫(xiě)入即可創(chuàng)建一個(gè)線(xiàn)程出來(lái)。。
87 View Code
?
轉(zhuǎn)載于:https://www.cnblogs.com/Wild-orangutans/p/3824366.html
總結(jié)
以上是生活随笔為你收集整理的iOS-多线程 ,整理集锦,多种线程的创建的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CentOS 6.4利用xampp安装b
- 下一篇: Git帮助文档阅读笔记----第二章