1.创建方法

使用NSThread创建线程主要有两个个方法,分别如下

  1. NSThread* myThread = [[NSThread alloc] initWithTarget:self   selector:@selector(doSomething:) object:nil];

    [myThread start];

  2. [NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:nil];

这两种方式的区别在于:

前一种方式尽管alloc了一个新Thread,但需要手动调用start方法来启动线程。这点与Java创建线程的方式相似。第二种方式,与上述做法1使用NSObject的类方法performSelectorInBackground:withObject:是一样的;第二种方式的可以在start真正创建线程之前对其进行设置,比如设置线程的优先级。第二种调用就会立即创建一个线程并执行selector方法。

使用NSThread创建线程时,要在执行的方法中自行管理内存,否则会有内存溢出警告。

- (void)doSomething:(id)sender

{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[pool release];

}

2.线程间的通讯

与主线程通讯

[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];

与指定的线程通讯

[viewDelegate performSelector:@selector(someMethod)

                                 onThread:[NSThread mainThread]
withObject:nil
waitUntilDone:NO]
3.线程同步
线程同步主要用
NSCondition 和 NSLock
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; tickets = ;
count = ;
theLock = [[NSLock alloc] init];
// 锁对象
ticketsCondition = [[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 *ticketsThreadthree = [[NSThread alloc] initWithTarget:self selector:@selector(run3) object:nil];
[ticketsThreadthree setName:@"Thread-3"];
[ticketsThreadthree start];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch. [self.window makeKeyAndVisible];
return YES;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
} -(void)run3
{
while (YES) {
[ticketsCondition lock];
[NSThread sleepForTimeInterval:];
[ticketsCondition signal];
[ticketsCondition unlock];
}
} - (void)run
{
while (TRUE) {
// 上锁
[ticketsCondition lock];
[ticketsCondition wait];
[theLock lock];
if(tickets >= ){
[NSThread sleepForTimeInterval:0.09];
count = - tickets;
NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThread currentThread] name]);
tickets--;
}else{
break;
}
[theLock unlock];
[ticketsCondition unlock];
}
}



最新文章

  1. C语言程序设计第六次作业
  2. 速战速决 (2) - PHP: 数据类型 bool, int, float, string, object, array
  3. IOS - UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte
  4. iOS设计模式之单例模式
  5. [原创]南水之源A*(A-Star)算法
  6. EXTJS API
  7. WP8教程
  8. [AngularJS] angular-formly: Extending Types
  9. Android(java)学习笔记259:JNI之NDK开发步骤
  10. Android Studio使用心得 - 简单介绍与环境配置
  11. quick-cocos2d-x添加到Pomelo的支持
  12. http调用端HttpClient、DefaultHttpClient、CloseableHttpClient
  13. java 实现微博,QQ联合登录
  14. Dropout, DropConnect ——一个对输出,一个对输入
  15. 兼容IE8的flash上传框架"uploadify"自定义上传按钮样式的办法
  16. linux tail -f messages查看控制台失败
  17. linux下postgresql的连接数配置
  18. linux 用户配制文件 用户增加及删除 以及用户属于的更改
  19. C# ArrayList的使用方法小总结
  20. Qt 的线程与事件循环

热门文章

  1. 【poj1090】 Chain
  2. slf4j和log4j配置
  3. LocalDB简介和在VS2012及以上版本的使用
  4. ci中如何得到配置的url
  5. [工作积累] Google/Amazon平台的各种坑
  6. 【原创】express3.4.8源码解析之中间件
  7. thinkphp中模块和操作映射
  8. DLL注入之SetWindowsHookEx
  9. DLL注入之注册表
  10. Apache同时支持PHP和Python的配置方法