[NSThread-Analysis of Multithreading
(2013-03-30 22:48:37) label: nsthreadUse of nsthreadxcodeiphoneit |
Category: Help Document |
Any iOS application is composed of one or more threads. No matter whether you use a multi-threaded programming techniques , there are at least 1 thread is created. This thread is called the "main UI thread " and is attached to the main event processing loop (main run loop) .
// Task 1
- (void) firstCounter{
NSUInteger counter = 0;
for (counter = 0;
counter < 1000;
counter++){
NSLog(@"First Counter = %lu", (unsigned long)counter); }
}
// Task 2
- (void) secondCounter{
NSUInteger counter = 0;
for (counter = 0;
counter < 1000;
counter++){
NSLog(@"Second Counter = %lu", (unsigned long)counter); }
}
// Task 3
- (void) thirdCounter{
NSUInteger counter = 0;
for (counter = 0;
counter < 1000;
counter++){
NSLog(@"Third Counter = %lu", (unsigned long)counter);
}
}
- (void)viewDidLoad{
[super viewDidLoad];
[self firstCounter];
[self secondCounter];
[self thirdCounter];
}
First Counter = 0
... ...
First Counter = 999
Second Counter = 0
... ...
Second Counter = 999
Third Counter = 0
... ...
Third Counter = 999
你会看到第一个计时器运行完毕,然后是第二个计时器,最后是第三个计时器。也就是说这些循环是在同一个线程运行的。线程代码中被执行的每一块代码一直在运行,直到循 环结束。
- (void)viewDidLoad{
[super viewDidLoad];
// 开辟一个线程,执行任务
[NSThread detachNewThreadSelector:@selector(firstCounter)
toTarget:self
withObject:nil];
// 开辟一个线程,执行任务
[NSThread detachNewThreadSelector:@selector(secondCounter)
toTarget:self
withObject:nil];
// 该方法在主线程中执行
[self thirdCounter];
}
First Counter = 997
Second Counter = 984
First Counter = 998
Second Counter = 985
First Counter = 999
贴:
[self performSelectorInBackground:@selector(firstCounter) withObject:nil];
[self performSelectorInBackground:@selector(secondCounter) withObject:nil];
[self performSelectorInBackground:@selector(thirdCounter) withObject:nil];
performSelectorInBackground方法为我们在后台创建了一个线程。这等同于 我们为 selectors 创建一个新的线程。 但是要记住,必须在调用的方法中加上自动释放池!
// 任务1
- (void) firstCounter{
@autoreleasepool {
// MyCode
}
}
// 线程执行
- (void) threadEntryPoint{
@autoreleasepool {
NSLog(@"Thread Entry Point");
while ([[NSThread currentThread] isCancelled] == NO){
[NSThread sleepForTimeInterval:10];
NSLog(@"Thread Loop");
}
NSLog(@"Thread Finished");
}
}
// 停止线程
- (void) stopThread{
NSLog(@"Cancelling the Thread");
[self.myThread cancel];
NSLog(@"Releasing the thread");
self.myThread = nil;
}
- (void)viewDidAppear:(BOOL)animated{
// 创建线程
self.myThread = [[NSThread alloc]
initWithTarget:self
selector:@selector(threadEntryPoint)
object:nil];
// 开启线程
[self.myThread start];
// 让线程3秒后取消
[self performSelector:@selector(stopThread) withObject:nil
afterDelay:3.0f];
}
Thread Entry Point
Cancelling the Thread
Releasing the thread
Thread Loop
Thread Finished
- (void) threadEntryPoint{
@autoreleasepool {
NSLog(@"Thread Entry Point");
while ([[NSThread currentThread] isCancelled] == NO){
[NSThread sleepForTimeInterval:10];
if ([[NSThread currentThread] isCancelled] == NO){
// 做一个改进,在需要执行的代码中,多加一层判断。
NSLog(@"Thread Loop");
}
}
NSLog(@"Thread Finished");
}
}