多线程的几中创建方法与基本使用
来源:互联网 发布:谁说菜鸟不会数据分析 编辑:程序博客网 时间:2024/05/22 00:14
- (
BOOL
)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
02
{
03
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
04
05
//创建线程的第一种方式
06
NSThread *
thread
= [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@
"universe"
];
07
[
thread
start];
08
[
thread
release];
09
10
11
//创建线程的第二种方式,NSThread类方法
12
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@
"yuzhou"
];
13
14
15
//创建线程的第三种方法 NSObject方法
16
[self performSelectorInBackground:@selector(run:) withObject:@
"nsobject thread"
];
17
18
//创建线程的第四种方式
19
NSOperationQueue *oprationQueue = [[NSOperationQueue alloc] init];
20
[oprationQueue addOperationWithBlock:^{
21
//这个block语句块在子线程中执行
22
NSLog(@
"oprationQueue"
);
23
}];
24
[oprationQueue release];
25
26
//第五种创建线程的方式
27
NSOperationQueue *oprationQueue1 = [[NSOperationQueue alloc] init];
28
oprationQueue1.maxConcurrentOperationCount = 1;
//指定池子的并发数
29
30
//NSOperation 相当于java中的runnable接口,继承自它的就相当一个任务
31
NSInvocationOperation *invation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run:) object:@
"invation"
];
32
[oprationQueue1 addOperation:invation];
//将任务添加到池子里面,可以给池子添加多个任务,并且指定它的并发数
33
[invation release];
34
35
//第二个任务
36
NSInvocationOperation *invation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run2:) object:@
"invocation2"
];
37
invation2.queuePriority = NSOperationQueuePriorityHigh;
//设置线程优先级
38
[oprationQueue1 addOperation:invation2];
39
[invation2 release];
40
41
[oprationQueue1 release];
42
43
//调用主线程,用来子线程和主线程交互,最后面的那个boolean参数,如果为yes就是等这个方法执行完了在执行后面的代码;如果为no的话,就是不管这个方法执行完了没有,接着往下走
44
[self performSelectorOnMainThread:@selector(onMain) withObject:self waitUntilDone:YES];
45
46
//---------------------GCD----------------------支持多核,高效率的多线程技术
47
//创建多线程第六种方式
48
dispatch_queue_t queue = dispatch_queue_create(
"name"
, NULL);
49
//创建一个子线程
50
dispatch_async(queue, ^{
51
// 子线程code... ..
52
NSLog(@
"GCD多线程"
);
53
54
//回到主线程
55
dispatch_sync(dispatch_get_main_queue(), ^{
//其实这个也是在子线程中执行的,只是把它放到了主线程的队列中
56
Boolean isMain = [NSThread isMainThread];
57
if
(isMain) {
58
NSLog(@
"GCD主线程"
);
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
78
- (
void
) run:(NSString*)str
79
{
80
NSLog(@
"多线程运行:::%@"
,str);
81
}
82
- (
void
) run2:(NSString*)str
83
{
84
NSLog(@
"多线程运行:::%@"
,str);
85
}
0 0
- 多线程的几中创建方法与基本使用
- 多线程中创建线程的几种方法
- Win32多线程的创建方法和基本使用
- Win32 多线程的创建方法和基本使用
- Win32 多线程的创建方法和基本使用
- Win32 多线程的创建方法和基本使用
- Win32 多线程的创建方法和基本使用
- Win32 多线程的创建方法和基本使用
- android 创建多线程的几种方法
- 多线程的几种创建方法
- Java创建多线程的几种方法
- android 创建多线程的几种方法
- iOS 创建多线程的几种方法
- android 创建多线程的几种方法
- qt创建多线程的几种方法
- iOS 的几种创建多线程方法
- 创建多线程的几种方法
- Android中创建与几种解析xml的方法!
- Waveform Audio 驱动(Wavedev2)之:WAV 驱动解析
- java 图片解析,血的教训 BitmapFactory.decodeByteArray() 是个邪恶的方法
- 程序员到架构师
- RMAN恢复操作
- 易语言的按钮事件(破解小技巧)
- 多线程的几中创建方法与基本使用
- Android Uiautomator创建过程
- SPFA算法求最短路径
- VPN 隧道的 GRE 协议 47 包说明和使用
- 在ubuntu中安装SVN客户端步骤
- 留住你的用户:5款完全免费的第三方移动推送服务
- VC 关于调试和多线程的编译选项
- Hibernate Criteria查询
- console.table()调试javascript