標籤:style java color 使用 os art
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; //建立線程的第一種方式 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"universe"]; [thread start]; [thread release]; //建立線程的第二種方式,NSThread類方法 [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"yuzhou"]; //建立線程的第三種方法 NSObject方法 [self performSelectorInBackground:@selector(run:) withObject:@"nsobject thread"]; //建立線程的第四種方式 NSOperationQueue *oprationQueue = [[NSOperationQueue alloc] init]; [oprationQueue addOperationWithBlock:^{ //這個block語句塊在子線程中執行 NSLog(@"oprationQueue"); }]; [oprationQueue release]; //第五種建立線程的方式 NSOperationQueue *oprationQueue1 = [[NSOperationQueue alloc] init]; oprationQueue1.maxConcurrentOperationCount = 1;//指定池子的並發數 //NSOperation 相當於java中的runnable介面,繼承自它的就相當一個任務 NSInvocationOperation *invation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run:) object:@"invation"]; [oprationQueue1 addOperation:invation];//將任務添加到池子裡面,可以給池子添加多個任務,並且指定它的並發數 [invation release]; //第二個任務 NSInvocationOperation *invation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run2:) object:@"invocation2"]; invation2.queuePriority = NSOperationQueuePriorityHigh;//設定線程優先順序 [oprationQueue1 addOperation:invation2]; [invation2 release]; [oprationQueue1 release]; //調用主線程,用來子線程和主線程互動,最後面的那個boolean參數,如果為yes就是等這個方法執行完了在執行後面的代碼;如果為no的話,就是不管這個方法執行完了沒有,接著往下走 [self performSelectorOnMainThread:@selector(onMain) withObject:self waitUntilDone:YES]; //---------------------GCD----------------------支援多核,高效率的多線程技術 //建立多線程第六種方式 dispatch_queue_t queue = dispatch_queue_create("name", NULL); //建立一個子線程 dispatch_async(queue, ^{ // 子線程code... .. NSLog(@"GCD多線程"); //回到主線程 dispatch_sync(dispatch_get_main_queue(), ^{//其實這個也是在子線程中執行的,只是把它放到了主線程的隊列中 Boolean isMain = [NSThread isMainThread]; if (isMain) { NSLog(@"GCD主線程"); } }); }); self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES;}- (void)onMain{ Boolean b = [NSThread isMainThread]; if (b) { NSLog(@"onMain;;%d",b); }}- (void) run:(NSString*)str{ NSLog(@"多線程運行:::%@",str);}- (void) run2:(NSString*)str{ NSLog(@"多線程運行:::%@",str);}
這些都是基本的線程建立,用NSThread來進行建立線程比較簡單,如果是單一的建立線程可以用NSThread。直接建立可以使用。只需把線程執行的函數和方法寫入即可建立一個線程出來。。