天天看點

iphone 使用多線程的方法

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.     int n = rand()%5 + 1;  
  5.     NSLog(@"@@@ Thread 111111 Will sleep %d seconds ,now _threadCount is : %d",n,_threadCount);  
  6.     sleep(n);  
  7.     //[NSThread sleepForTimeInterval:n];  
  8.     _threadCount ++ ;  
  9.     NSLog(@"@@@ Thread 111111 has sleep %d seconds ,now _threadCount is : %d",n,_threadCount);  
  10.     [_myCondition signal];  
  11.     NSLog(@"@@@ Thread 1111111 has signaled ,now _threadCount is : %d",_threadCount);  
  12.     [_myCondition unlock];  
  13.     NSLog(@"@@@ In thread one complete.");  
  14.     [NSThread exit];  
  15.     return;  
  16. }  

繼續閱讀