【转】IOS里的多线程编程详解

简介:

以下是开发初期收集整理的一点资料 

多线程之NSInvocationOperation
多线程编程是防止主线程堵塞,增加运行效率等等的最佳方法。而原始的多线程方法存在很多的毛病,包括线程锁死等。在Cocoa中,Apple提供了NSOperation这个类,提供了一个优秀的多线程编程方法。
本次介绍NSOperation的子集,简易方法的NSInvocationOperation:
@implementation MyCustomClass
 - (void)launchTaskWithData:(id)data
{
    //创建一个NSInvocationOperation对象,并初始化到方法
    //在这里,selector参数后的值是你想在另外一个线程中运行的方法(函数,Method)
    //在这里,object后的值是想传递给前面方法的数据
    NSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:self
                    selector:@selector(myTaskMethod:) object:data];
 
    // 下面将我们建立的操作“Operation”加入到本地程序的共享队列中(加入后方法就会立刻被执行)
    // 更多的时候是由我们自己建立“操作”队列
    [[MyAppDelegate sharedOperationQueue] addOperation:theOp];
}
 
// 这个是真正运行在另外一个线程的“方法”
- (void)myTaskMethod:(id)data
{
    // Perform the task.
}
 
@end一个NSOperationQueue 操作队列,就相当于一个线程管理器,而非一个线程。因为你可以设置这个线程管理器内可以并行运行的的线程数量等等。下面是建立并初始化一个操作队列:

@interface MyViewController : UIViewController {
 
    NSOperationQueue *operationQueue;
    //在头文件中声明该队列
}
@end
 
@implementation MyViewController
 
- (id)init 
{
    self = [super init];
    if (self) {
        operationQueue = [[NSOperationQueue alloc] init]; //初始化操作队列
        [operationQueue setMaxConcurrentOperationCount:1];
        //在这里限定了该队列只同时运行一个线程
        //这个队列已经可以使用了
    }
    return self;
}
 
- (void)dealloc
{
    [operationQueue release];
    //正如Alan经常说的,我们是程序的好公民,需要释放内存!
    [super dealloc];
}
 
@end简单介绍之后,其实可以发现这种方法是非常简单的。很多的时候我们使用多线程仅仅是为了防止主线程堵塞,而NSInvocationOperation就是最简单的多线程编程,在iPhone编程中是经常被用到的。



///////////////////////////////////////////////////////////////////////////////////////////////////
1 在主线程里加入一个loading画面……
2 {
3 [window addSubview:view_loading];
4 [NSThread detachNewThreadSelector:@selector(init_backup:) toTarget:self withObject:nil];
5 }
可以通过performSelectorOhMainThread更新UI元素,比如设置进度条等等。最后消除loading画面,载入主View。
7 - (void)init_backup:(id)sender
8 {
9 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
10 
11 // ...
12 int i = status;
13 [self performSelectorOnMainThread:@selector(show_loading:) withObject:[NSNumber numberWithInt:i] waitUntil Done:NO];
14 
15 [view_loading removeFromSuperview];
16 [window addSubview:tabcontroller_main.view];
17 [pool release];
18 }

 

///////////////////////////////////////////////////////

利用iphone的多线程实现和线程同步

从接口的定义中可以知道,NSThread和大多数iphone的接口对象一样,有两种方式可以初始化:

一种使用initWithTarget :(id)target selector:(SEL)selector object:(id)argument,但需要负责在对象的retain count为0时调用对象的release方法清理对象。

另一种则使用所谓的convenient method,这个方便接口就是detachNewThreadSelector,这个方法可以直接生成一个线程并启动它,而且无需为线程的清理负责。

#import <UIKit/UIKit.h>
@interface SellTicketsAppDelegate : NSObject <UIApplicationDelegate> {
    int tickets;
    int count;
    NSThread* ticketsThreadone;
    NSThread* ticketsThreadtwo;
    NSCondition* ticketsCondition;
    UIWindow *window;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@end

 

然后在实现中添加如下代码:
//  SellTicketsAppDelegate.m
//  SellTickets
//
//  Created by sun dfsun2009 on 09-11-10.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//
#import "SellTicketsAppDelegate.h"
@implementation SellTicketsAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
    tickets = 100;
    count = 0;
    // 锁对象
    ticketCondition = [[NSCondition alloc] init];
    ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
    [ticketsThreadone setName:@"Thread-1"];
    [ticketsThreadone start];
    ticketsThreadtwo = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
    [ticketsThreadtwo setName:@"Thread-2"];
    [ticketsThreadtwo start];
    //[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
    // Override point for customization after application launch
    [window makeKeyAndVisible];
}
- (void)run{
    while (TRUE) {
        // 上锁
        [ticketsCondition lock];
        if(tickets > 0)
        {
            [NSThread sleepForTimeInterval:0.5];
            count = 100 - tickets;
            NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThread currentThread] name]);
            tickets--;
        }else
        {
            break;
        }
        [ticketsCondition unlock];
    }
}
-         (void)dealloc {
    [ticketsThreadone release];
    [ticketsThreadtwo release];
    [ticketsCondition release];
    [window release];
    [super dealloc];
}
@end

-------------------------------------------------------------------------------------
// 定义
#import <UIKit/UIKit.h>

@interface ThreadSyncSampleViewController : UIViewController {
 int _threadCount;
 NSCondition *_myCondition;
}

@end

//实现文件如下:

#import "ThreadSyncSampleViewController.h"

@implementation ThreadSyncSampleViewController

 

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
 //
 //_myCondition = nil;
 //
 _myCondition = [[NSCondition alloc] init];
 //
 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:30 
             target:self
              selector:@selector(threadTester)
              userInfo:nil
             repeats:YES];
 [timer fire];
 
}


- (void)threadTester{
 [_myCondition lock];
 
 _threadCount = -2;
 //如果有n个要等待的thread,这里置成 -n
 [_myCondition unlock];
 //
 NSLog(@"");
 NSLog(@"------------------------------------------------------------------------------");
 [NSThread detachNewThreadSelector:@selector(threadOne) toTarget:self withObject:nil];
 [NSThread detachNewThreadSelector:@selector(threadTwo) toTarget:self withObject:nil];
 [NSThread detachNewThreadSelector:@selector(threadThree) toTarget:self withObject:nil];
 return;
}

- (void)threadOne{
 NSLog(@"@@@ In thread 111111 start.");
 [_myCondition lock];
 
 int n = rand()%5 + 1;
 NSLog(@"@@@ Thread 111111 Will sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
 sleep(n);
 //[NSThread sleepForTimeInterval:n];
 _threadCount ++ ;
 NSLog(@"@@@ Thread 111111 has sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
 [_myCondition signal];
 NSLog(@"@@@ Thread 1111111 has signaled ,now _threadCount is : %d",_threadCount);
 [_myCondition unlock];
 NSLog(@"@@@ In thread one complete.");
 [NSThread exit];
 return;
}

- (void)threadTwo{
 NSLog(@"### In thread 2222222 start.");
 [_myCondition lock];
 
 int n = rand()%5 + 1;
 NSLog(@"### Thread 2222222 Will sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
 sleep(n);
 //   [NSThread sleepForTimeInterval:n];
 _threadCount ++ ;
 NSLog(@"### Thread 2222222 has sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
 [_myCondition signal];
 NSLog(@"### Thread 2222222 has signaled ,now _threadCount is : %d",_threadCount);
 [_myCondition unlock];
 //_threadCount ++ ;
 NSLog(@"### In thread 2222222 complete.");
 [NSThread exit];
 return;
}

- (void)threadThree{
 NSLog(@"<<< In thread 333333 start.");
 [_myCondition lock];
 while (_threadCount < 0) {
  [_myCondition wait];
 }
 NSLog(@"<<< In thread 333333 ,_threadCount now is %d ,will start work.",_threadCount);
 [_myCondition unlock];
 NSLog(@"<<< In thread 333333 complete.");
 [NSThread exit];
 return;
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
 
 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
 [_myCondition release];
    [super dealloc];
}

本文转自编程小翁博客园博客,原文链接:http://www.cnblogs.com/wengzilin/archive/2012/08/15/2639576.html,如需转载请自行联系原作者

相关文章
|
5天前
|
安全 Java 调度
Java编程时多线程操作单核服务器可以不加锁吗?
Java编程时多线程操作单核服务器可以不加锁吗?
18 2
|
10天前
|
Java 调度 开发者
Java中的多线程编程:从基础到实践
本文旨在深入探讨Java多线程编程的核心概念和实际应用,通过浅显易懂的语言解释多线程的基本原理,并结合实例展示如何在Java中创建、控制和管理线程。我们将从简单的线程创建开始,逐步深入到线程同步、通信以及死锁问题的解决方案,最终通过具体的代码示例来加深理解。无论您是Java初学者还是希望提升多线程编程技能的开发者,本文都将为您提供有价值的见解和实用的技巧。
15 2
|
12天前
|
Java 数据处理
Java中的多线程编程:从基础到实践
本文旨在深入探讨Java中的多线程编程,涵盖其基本概念、创建方法、同步机制及实际应用。通过对多线程基础知识的介绍和具体示例的演示,希望帮助读者更好地理解和应用Java多线程编程,提高程序的效率和性能。
19 1
|
15天前
|
安全 Java 调度
Java 并发编程中的线程安全和性能优化
本文将深入探讨Java并发编程中的关键概念,包括线程安全、同步机制以及性能优化。我们将从基础入手,逐步解析高级技术,并通过实例展示如何在实际开发中应用这些知识。阅读完本文后,读者将对如何在多线程环境中编写高效且安全的Java代码有一个全面的了解。
|
5天前
|
Java
COMATE插件实现使用线程池高级并发模型简化多线程编程
本文介绍了COMATE插件的使用,该插件通过线程池实现高级并发模型,简化了多线程编程的过程,并提供了生成结果和代码参考。
|
7天前
|
Java 数据处理 调度
Java中的多线程编程:从基础到实践
本文深入探讨了Java中多线程编程的基本概念、实现方式及其在实际项目中的应用。首先,我们将了解什么是线程以及为何需要多线程编程。接着,文章将详细介绍如何在Java中创建和管理线程,包括继承Thread类、实现Runnable接口以及使用Executor框架等方法。此外,我们还将讨论线程同步和通信的问题,如互斥锁、信号量、条件变量等。最后,通过具体的示例展示了如何在实际项目中有效地利用多线程提高程序的性能和响应能力。
|
8天前
|
安全 算法 Java
Java中的多线程编程:从基础到高级应用
本文深入探讨了Java中的多线程编程,从最基础的概念入手,逐步引导读者了解并掌握多线程开发的核心技术。无论是初学者还是有一定经验的开发者,都能从中获益。通过实例和代码示例,本文详细讲解了线程的创建与管理、同步与锁机制、线程间通信以及高级并发工具等主题。此外,还讨论了多线程编程中常见的问题及其解决方案,帮助读者编写出高效、安全的多线程应用程序。
|
9天前
|
安全 Java 调度
python3多线程实战(python3经典编程案例)
该文章提供了Python3中多线程的应用实例,展示了如何利用Python的threading模块来创建和管理线程,以实现并发执行任务。
12 0
|
10天前
|
存储 缓存 Java
JAVA并发编程系列(11)线程池底层原理架构剖析
本文详细解析了Java线程池的核心参数及其意义,包括核心线程数量(corePoolSize)、最大线程数量(maximumPoolSize)、线程空闲时间(keepAliveTime)、任务存储队列(workQueue)、线程工厂(threadFactory)及拒绝策略(handler)。此外,还介绍了四种常见的线程池:可缓存线程池(newCachedThreadPool)、定时调度线程池(newScheduledThreadPool)、单线程池(newSingleThreadExecutor)及固定长度线程池(newFixedThreadPool)。
|
15天前
|
安全 数据库连接 API
C#一分钟浅谈:多线程编程入门
在现代软件开发中,多线程编程对于提升程序响应性和执行效率至关重要。本文从基础概念入手,详细探讨了C#中的多线程技术,包括线程创建、管理及常见问题的解决策略,如线程安全、死锁和资源泄露等,并通过具体示例帮助读者理解和应用这些技巧,适合初学者快速掌握C#多线程编程。
49 0
下一篇
无影云桌面