自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone開(kāi)發(fā)之多線程N(yùn)SThread和NSInvocationOperation

移動(dòng)開(kāi)發(fā) iOS
多線程編程是防止主線程堵塞,增加運(yùn)行效率等等的最佳方法。而原始的多線程方法存在很多的毛病,包括線程鎖死等。在Cocoa中,Apple提供了NSOperation這個(gè)類(lèi),提供了一個(gè)優(yōu)秀的多線程編程方法。

多線程編程是防止主線程堵塞,增加運(yùn)行效率等等的***方法。而原始的多線程方法存在很多的毛病,包括線程鎖死等。在Cocoa中,Apple提供了NSOperation這個(gè)類(lèi),提供了一個(gè)優(yōu)秀的多線程編程方法。

本次介紹NSOperation的子集,簡(jiǎn)易方法的NSInvocationOperation:

 

 

  1. @implementation MyCustomClass   
  2.  
  3.  -(void)launchTaskWithData:(id)data   
  4.  
  5.  {   
  6.  
  7.    //創(chuàng)建一個(gè)NSInvocationOperation對(duì)象,并初始化到方法;   
  8.  
  9.     //在這里,selector參數(shù)后的值是你想在另外一個(gè)線程中運(yùn)行的方法(函數(shù),Method);   
  10.  
  11.    //在這里,object后的值是想傳遞給前面方法的數(shù)據(jù)   
  12.  
  13.    NSInvocationOperation *theOp = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(myTashMethod:) object:data];   
  14.  
  15.    //下面將我們建立的操作"Operation"加入到本地程序的共享的隊(duì)列中(加入后方法就會(huì)立刻被執(zhí)行)   
  16.  
  17.    //更多的時(shí)候是由我們自己建立“操作”隊(duì)列   
  18.  
  19.    [[MyAppDelegate sharedOperationQueue] addOperation:theOp];   
  20.  
  21.  }   
  22.  
  23.     
  24.  
  25.  //這個(gè)是真正運(yùn)行在另外一個(gè)線程的“方法”   
  26.  
  27.     
  28.  
  29.  - (void)myTaskMethod:(id)data   
  30.  
  31. {   
  32.     // Perform the task.   
  33.  
  34.  }   
  35.  
  36.  @end   
  37.  
  38.     
  39.  
  40. //一個(gè)NSOperationQueue 操作隊(duì)列,就相當(dāng)于一個(gè)線程管理器,而非一個(gè)線程。因?yàn)槟憧梢栽O(shè)置這個(gè)線程管理器內(nèi)可以并行運(yùn)行的的線程數(shù)量等等。   
  41.  
  42.  //下面是建立并初始化一個(gè)操作隊(duì)列:   
  43.  
  44.  @interface MyViewController : UIViewController {   
  45.  
  46.     
  47.  
  48.     NSOperationQueue* operationQueue;   
  49.  
  50.    //在頭文件中聲明該隊(duì)列   
  51.  
  52. }   
  53.  
  54.  @end   
  55.  
  56.     
  57.  
  58.  @implementation MyViewController   
  59.  
  60.     
  61.  
  62.  -(id)init   
  63.  {   
  64.  
  65.   self = [super init];   
  66.  
  67.    if (self) {   
  68.  
  69.       //初始化操作隊(duì)列   
  70.  
  71.       operationQueue = [[NSOperationQueue alloc] init];   
  72.  
  73.       [operationQueue setMaxConcurrentOperationCount:1];   
  74.  
  75.        //在這里限定了該隊(duì)列只同時(shí)運(yùn)行一個(gè)線程   
  76.  
  77.       //這個(gè)隊(duì)列已經(jīng)可以使用了   
  78.  
  79.    }   
  80.  
  81.     return self;   
  82.  
  83. }   
  84.  
  85.     
  86.  
  87.  - (void)dealloc   
  88.  
  89.  {   
  90.  
  91.     [operationQueue release];   
  92.  
  93.     [super dealloc];   
  94.  
  95. }   
  96.  
  97.  @end   
  98.  
  99. //簡(jiǎn)單介紹之后,其實(shí)可以發(fā)現(xiàn)這種方法是非常簡(jiǎn)單的。很多的時(shí)候我們使用多線程僅僅是為了防止主線程堵塞,而NSInvocationOperation就是最簡(jiǎn)單的多線程編程,在iPhone編程中是經(jīng)常被用到的。   
  100.  
  101.     
  102.  
  103. /////////////////////////////////////////////////////////////   
  104.  
  105.  //在主線程里加入一個(gè)loading畫(huà)面……   
  106.  
  107.  {   
  108.  
  109.     [window addSubview:view_loading];   
  110.  
  111.     //另一個(gè)新的線程,可能需要時(shí)間進(jìn)行后臺(tái)處理,為了防止主程序在這段時(shí)間靜止等待,將后臺(tái)處理放在主線程之外的線程執(zhí)行,執(zhí)行完以后,通知主線程更新數(shù)據(jù)。   
  112.  
  113.     [NSThread detachNewThreadSelector:@selector:(init_backup:) toTarget:self withObject:nil];   
  114.  
  115.  }   
  116.  
  117.     
  118.  
  119.      
  120.  
  121.  //可以通過(guò)performSelectorOhMainThread更新UI元素,比如設(shè)置進(jìn)度條等等。***消除loading畫(huà)面,載入主View。   
  122.  
  123.  -(void)init_backup:(id)sender   
  124.  
  125.  {   
  126.  
  127.     NSAutorelease* pool = [[NSAutoreleasePool alloc] init];   
  128.  
  129.    //新建的線程需要一個(gè)自動(dòng)釋放池對(duì)線程中申請(qǐng)的內(nèi)存進(jìn)行管理   
  130.  
  131.     int i = status;   
  132.  
  133.     [self performSelectorOnMainThread:@selector:(show_loading:) wiwithObject::[NSNumber numberWithInt:i] waitUntil Done:NO];   
  134.  
  135.     [view_loading removeFromSuperview];   
  136.  
  137.     [window addSubview:tabcontroller_main.view];   
  138.  
  139.    [pool release];   
  140.  

利用iphone的多線程實(shí)現(xiàn)和線程同步

    從接口的定義中可以知道,NSThread和大多數(shù)iphone的接口對(duì)象一樣,有兩種方式可以初始化:

    一種使用initWithTarget :(id)target selector:(SEL)selector object:(id)argument,但需要負(fù)責(zé)在對(duì)象的retain count為0時(shí)調(diào)用對(duì)象的release方法清理對(duì)象。

   另一種則使用所謂的convenient method,這個(gè)方便接口就是detachNewThreadSelector,這個(gè)方法可以直接生成一個(gè)線程并啟動(dòng)它,而且無(wú)需為線程的清理負(fù)責(zé)。

  1. #import <UIKit/UIKit.h>   
  2.  
  3.  @interface SellTicketsAppDelegate : NSObject <UIApplicationDelegate>    
  4.  
  5. {   
  6.  
  7.      
  8.  
  9.     int tickets;   
  10.  
  11.     int count;   
  12.  
  13.      NSThread* ticketsThreadone;   
  14.  
  15.     NSThread* ticketsThreadtwo;   
  16.  
  17.     NSCondition* ticketsCondition;   
  18.  
  19.      UIWindow *window;   
  20.  
  21.  }   
  22.  
  23.  @property (nonatomic, retain) IBOutlet UIWindow *window;   
  24.  
  25.  @end   
  26.  
  27.    
  28.  
  29.  //SellTicketsAppDelegate.m   
  30.  
  31.  #import "SellTicketsAppDelegate.h"   
  32.  @implementation SellTicketsAppDelegate   
  33.  
  34.  @synthesize window;   
  35.  
  36.  - (void)applicationDidFinishLaunching:(UIApplication *)application    
  37.  
  38.  {   
  39.  
  40.    
  41.  
  42.    tickets = 100;   
  43.  
  44.     count = 0;   
  45.  
  46.     // 鎖對(duì)象   
  47.  
  48.     ticketCondition = [[NSCondition alloc] init];   
  49.  
  50.     ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];   
  51.  
  52.    [ticketsThreadone setName:@"Thread-1"];   
  53.  
  54.    [ticketsThreadone start];   
  55.  
  56.     
  57.  
  58.     ticketsThreadtwo = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];   
  59.  
  60.     [ticketsThreadtwo setName:@"Thread-2"];   
  61.  
  62.     [ticketsThreadtwo start];      
  63.  
  64.     
  65.  
  66.     //[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];   
  67.  
  68.     // Override point for customization after application launch   
  69.  
  70.     [window makeKeyAndVisible];   
  71.  
  72.  }   
  73.  
  74.      
  75.  
  76.  - (void)run{   
  77.  
  78.     while (TRUE) {   
  79.  
  80.      //上鎖   
  81.  
  82.       [ticketsCondition lock];   
  83.  
  84.       if(tickets > 0)   
  85.  
  86.      {   
  87.  
  88.           [NSThread sleepForTimeInterval:0.5];   
  89.  
  90.          count = 100 - tickets;   
  91.  
  92.          NSLog(@"當(dāng)前票數(shù)是:%d,售出:%d,線程名:%@",tickets,count,[[NSThread currentThread] name]);   
  93.  
  94.           tickets--;   
  95.  
  96.       }   
  97.  
  98.       else  
  99.  
  100.       {   
  101.  
  102.            break;   
  103.  
  104.       }   
  105.  
  106.       [ticketsCondition unlock];   
  107.  
  108.  }   
  109.  
  110.      
  111.  
  112.  -(void)dealloc{   
  113.  
  114.      [ticketsThreadone release];   
  115.  
  116.      [ticketsThreadtwo release];   
  117.  
  118.      [ticketsCondition release];   
  119.  
  120.      [window release];   
  121.  
  122.      [super dealloc];   
  123.  
  124.  }   
  125.  
  126. @end  
責(zé)任編輯:張葉青 來(lái)源: 開(kāi)源社區(qū)
相關(guān)推薦

2011-08-18 17:07:23

IOS開(kāi)發(fā)多線程NSInvocatio

2011-08-12 10:09:23

iPhone開(kāi)發(fā)多線程

2016-04-12 09:48:24

nsthread多線程ios

2023-06-13 13:39:00

多線程異步編程

2013-06-07 16:30:08

iOS多線程iOS開(kāi)發(fā)NSThread

2023-06-05 07:56:10

線程分配處理器

2023-06-06 08:17:52

多線程編程Thread類(lèi)

2011-06-02 17:27:49

iphone 多線程

2011-07-21 11:12:58

iPhone 線程 多線程

2011-08-08 13:50:29

iPhone開(kāi)發(fā) NSOperatio 多線程

2021-03-05 07:38:52

C++線程編程開(kāi)發(fā)技術(shù)

2009-08-17 16:56:51

C#多線程控制進(jìn)度條

2011-08-01 12:53:25

iPhone 多線程 線程

2011-07-08 16:43:46

iPhone Cocoa 多線程

2021-06-29 07:47:23

多線程協(xié)作數(shù)據(jù)

2011-06-07 17:35:39

iphone 多線程

2018-04-20 14:11:27

多線程死鎖樂(lè)觀鎖

2016-10-09 20:15:30

多線程多進(jìn)程

2023-11-03 07:50:01

2011-08-09 14:24:18

iPhone多線程線程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)