多线程 创建-同步-线程锁-交互-线程池
2012-01-08 02:10
323 查看
iPhone 多线程
多线程在各种编程语言中都是难点,很多语言中实现起来很麻烦,objective-c虽然源于c,但其多线程编程却相当简单,可以与java相媲美。这篇文章主要从线程创建与启动、线程的同步与锁、线程的交互、线程池等等四个方面简单的讲解一下iphone中的多线程编程。
一、线程创建与启动
线程创建主要有二种方式:
- (id)init; // designated initializer
- (id)initWithTarget:(id)target selector:(SEL)selectorobject:(id)argument;
当然,还有一种比较特殊,就是使用所谓的convenientmethod,这个方法可以直接生成一个线程并启动它,而且无需为线程的清理负责。
这个方法的接口是:
+ (void)detachNewThreadSelector:(SEL)aSelectortoTarget:(id)aTarget withObject:(id)anArgument
前两种方法创建后,需要手机启动,启动的方法是:
- (void)start;
二、线程的同步与锁
要说明线程的同步与锁,最好的例子可能就是多个窗口同时售票的售票系统了。
我们知道在java中,使用synchronized来同步,而iphone虽然没有提供类似java下的synchronized关键字,但提供了NSCondition对象接口。查看NSCondition的接口说明可以看出,NSCondition是iphone下的锁对象,所以我们可以使用NSCondition实现iphone中的线程安全。
这是来源于网上的一个例子:
SellTicketsAppDelegate.h 文件
// SellTicketsAppDelegate.h
@interface SellTicketsAppDelegate : NSObject {
int tickets;
int count;
NSThread* ticketsThreadone;
NSThread* ticketsThreadtwo;
NSCondition* ticketsCondition;
UIWindow *window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
SellTicketsAppDelegate.m 文件
// SellTicketsAppDelegate.m
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:selfselector:@selector(run) object:nil];
[ticketsThreadtwo setName:@"Thread-2"];
[ticketsThreadtwo start];
//[NSThread detachNewThreadSelector:@selector(run)toTarget:self withObject:nil];
// Override point for customization after applicationlaunch
[window makeKeyAndVisible];
}
- (void)run{
while (TRUE) {
// 上锁
[ticketsCondition lock];
if(tickets > 0){
[NSThreadsleepForTimeInterval:0.5];
count = 100 -tickets;
NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThreadcurrentThread] name]);
tickets--;
}else{
break;
}
[ticketsConditionunlock];
}
}
- (void)dealloc {
[ticketsThreadone release];
[ticketsThreadtwo release];
[ticketsCondition release];
[window release];
[super dealloc];
}
@end
三、线程的交互
线程在运行过程中,可能需要与其它线程进行通信,如在主线程中修改界面等等,可以使用如下接口:
- (void)performSelectorOnMainThread:(SEL)aSelectorwithObject:(id)arg waitUntilDone:(BOOL)wait
由于在本过程中,可能需要释放一些资源,则需要使用NSAutoreleasePool来进行管理,如:
- (void)startTheBackgroundJob {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
// to do something in your thread job
...
[selfperformSelectorOnMainThread:@selector(makeMyProgressBarMoving)withObject:nil waitUntilDone:NO];
[pool release];
}
稍后我将做一个更加详细的demo 给大家
多线程在各种编程语言中都是难点,很多语言中实现起来很麻烦,objective-c虽然源于c,但其多线程编程却相当简单,可以与java相媲美。这篇文章主要从线程创建与启动、线程的同步与锁、线程的交互、线程池等等四个方面简单的讲解一下iphone中的多线程编程。
一、线程创建与启动
线程创建主要有二种方式:
- (id)init; // designated initializer
- (id)initWithTarget:(id)target selector:(SEL)selectorobject:(id)argument;
当然,还有一种比较特殊,就是使用所谓的convenientmethod,这个方法可以直接生成一个线程并启动它,而且无需为线程的清理负责。
这个方法的接口是:
+ (void)detachNewThreadSelector:(SEL)aSelectortoTarget:(id)aTarget withObject:(id)anArgument
前两种方法创建后,需要手机启动,启动的方法是:
- (void)start;
二、线程的同步与锁
要说明线程的同步与锁,最好的例子可能就是多个窗口同时售票的售票系统了。
我们知道在java中,使用synchronized来同步,而iphone虽然没有提供类似java下的synchronized关键字,但提供了NSCondition对象接口。查看NSCondition的接口说明可以看出,NSCondition是iphone下的锁对象,所以我们可以使用NSCondition实现iphone中的线程安全。
这是来源于网上的一个例子:
SellTicketsAppDelegate.h 文件
// SellTicketsAppDelegate.h
@interface SellTicketsAppDelegate : NSObject {
int tickets;
int count;
NSThread* ticketsThreadone;
NSThread* ticketsThreadtwo;
NSCondition* ticketsCondition;
UIWindow *window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
SellTicketsAppDelegate.m 文件
// SellTicketsAppDelegate.m
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:selfselector:@selector(run) object:nil];
[ticketsThreadtwo setName:@"Thread-2"];
[ticketsThreadtwo start];
//[NSThread detachNewThreadSelector:@selector(run)toTarget:self withObject:nil];
// Override point for customization after applicationlaunch
[window makeKeyAndVisible];
}
- (void)run{
while (TRUE) {
// 上锁
[ticketsCondition lock];
if(tickets > 0){
[NSThreadsleepForTimeInterval:0.5];
count = 100 -tickets;
NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThreadcurrentThread] name]);
tickets--;
}else{
break;
}
[ticketsConditionunlock];
}
}
- (void)dealloc {
[ticketsThreadone release];
[ticketsThreadtwo release];
[ticketsCondition release];
[window release];
[super dealloc];
}
@end
三、线程的交互
线程在运行过程中,可能需要与其它线程进行通信,如在主线程中修改界面等等,可以使用如下接口:
- (void)performSelectorOnMainThread:(SEL)aSelectorwithObject:(id)arg waitUntilDone:(BOOL)wait
由于在本过程中,可能需要释放一些资源,则需要使用NSAutoreleasePool来进行管理,如:
- (void)startTheBackgroundJob {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
// to do something in your thread job
...
[selfperformSelectorOnMainThread:@selector(makeMyProgressBarMoving)withObject:nil waitUntilDone:NO];
[pool release];
}
稍后我将做一个更加详细的demo 给大家
相关文章推荐
- 04.多线程--06.【同步方式在线程两种创建方式中的可行性】【同步代码块和同步函数的关系】【多线程程序设计思路总结】
- MFC 线程创建、多线程、及多线程间的同步问题
- 黑马程序员------多线程(No.1)(概述、线程的创建、安全问题、同步锁、同步函数)
- JAVA笔记14__多线程共享数据(同步)/ 线程死锁 / 生产者与消费者应用案例 / 线程池
- Java知识点整理:第十四章:java多线程编程、创建一个线程、异步与同步、java反射、类加载
- 基础学习day11--多线程一线程的创建,运行,同步和锁
- Java笔记3 多线程<1>线程概述、多线程的创建、多线程的安全问题、静态同步函数的锁、死锁
- JAVA基础学习(十一)--多线程一线程的创建,运行,同步和锁
- 多线程介绍 线程池 Thread 线程创建 线程状态图
- 多线程(线程的概述,创建线程,控制线程,线程同步,线程池)
- java线程一之创建线程、线程池以及多线程运行时间统计
- 多线程编程之四——线程的同步
- 牛客网Java刷题知识点之什么是进程、什么是线程、什么是多线程、多线程的好处和弊端、多线程的创建方式、JVM中的多线程解析、多线程运行图解
- 本文一个例子展开,介绍Linux下面线程的操作、多线程的同步和互斥
- 多线程(九):线程相关知识点(创建方式,API,守护线程,状态转换)
- 【java多线程与并发库】---传统java多线程<2> 线程创建方式
- 多线程编程之四——线程的同步
- 【IO进程】守护进程,线程创建、同步与互斥
- 《 Java并发编程从入门到精通》 多线程之间交互:线程阀
- 嵌入式Linux应用程序开发——多线程3(线程的同步——条件变量)