iphone 使用多线程的方法

来源:互联网 发布:和君咨询怎么样知乎 编辑:程序博客网 时间:2024/06/05 19:17

http://blog.csdn.net/yuri99/article/details/5787626


创建一条线程还是比较简单的.

我要用的线程只是用来处理接收数据,不是用来处理ui上的动画的.

 

 

view plain
  1. [NSThread detachNewThreadSelector:@selector(threadOne) toTarget:self withObject:nil];  

 

为了保证线程中数据的同步,可以使用NSCondition来处理

view plain
  1. - (void)threadOne{  
  2.     NSLog(@"@@@ In thread 111111 start.");  
  3.     [_myCondition lock];  
  4.       
  5.     int n = rand()%5 + 1;  
  6.     NSLog(@"@@@ Thread 111111 Will sleep %d seconds ,now _threadCount is : %d",n,_threadCount);  
  7.     sleep(n);  
  8.     //[NSThread sleepForTimeInterval:n];  
  9.     _threadCount ++ ;  
  10.     NSLog(@"@@@ Thread 111111 has sleep %d seconds ,now _threadCount is : %d",n,_threadCount);  
  11.     [_myCondition signal];  
  12.     NSLog(@"@@@ Thread 1111111 has signaled ,now _threadCount is : %d",_threadCount);  
  13.     [_myCondition unlock];  
  14.     NSLog(@"@@@ In thread one complete.");  
  15.     [NSThread exit];  
  16.     return;  
  17. }  


原创粉丝点击