{"id":35,"date":"2012-08-13T17:57:12","date_gmt":"2012-08-13T09:57:12","guid":{"rendered":"http:\/\/sunyuping.cn\/?p=35"},"modified":"2012-08-13T17:57:12","modified_gmt":"2012-08-13T09:57:12","slug":"iphone-%e7%ba%bf%e7%a8%8b-nscondition-nsthread","status":"publish","type":"post","link":"https:\/\/www.sunyuping.com\/index.php\/2012\/08\/13\/iphone-%e7%ba%bf%e7%a8%8b-nscondition-nsthread\/","title":{"rendered":"iphone \u7ebf\u7a0b NSCondition NSThread"},"content":{"rendered":"<p>\t\t\t\t\u4e00\u3001\u7ebf\u7a0b\u521b\u5efa\u4e0e\u542f\u52a8<\/p>\n<p>\u7ebf\u7a0b\u521b\u5efa\u4e3b\u8981\u6709\u4e8c\u79cd\u65b9\u5f0f\uff1a<\/p>\n<pre>- (id)init; \/\/ designated initializer\n- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;<\/pre>\n<p>\u5f53\u7136\uff0c\u8fd8\u6709\u4e00\u79cd\u6bd4\u8f83\u7279\u6b8a\uff0c\u5c31\u662f\u4f7f\u7528\u6240\u8c13\u7684convenient method\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u751f\u6210\u4e00\u4e2a\u7ebf\u7a0b\u5e76\u542f\u52a8\u5b83\uff0c\u800c\u4e14\u65e0\u9700\u4e3a\u7ebf\u7a0b\u7684\u6e05\u7406\u8d1f\u8d23\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7684\u63a5\u53e3\u662f\uff1a<\/p>\n<div>\n<div>\n<pre>+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument<\/pre>\n<\/div>\n<\/div>\n<p>\u524d\u4e24\u79cd\u65b9\u6cd5\u521b\u5efa\u540e\uff0c\u9700\u8981\u624b\u673a\u542f\u52a8\uff0c\u542f\u52a8\u7684\u65b9\u6cd5\u662f\uff1a<\/p>\n<div>\n<div>\n<pre>- (void)start;<\/pre>\n<\/div>\n<\/div>\n<p><strong>\u4e8c\u3001\u7ebf\u7a0b\u7684\u540c\u6b65\u4e0e\u9501<\/strong><br \/>\n\u8981\u8bf4\u660e\u7ebf\u7a0b\u7684\u540c\u6b65\u4e0e\u9501\uff0c\u6700\u597d\u7684\u4f8b\u5b50\u53ef\u80fd\u5c31\u662f\u591a\u4e2a\u7a97\u53e3\u540c\u65f6\u552e\u7968\u7684\u552e\u7968\u7cfb\u7edf\u4e86\u3002\u6211\u4eec\u77e5\u9053\u5728java\u4e2d\uff0c\u4f7f\u7528synchronized\u6765\u540c\u6b65\uff0c\u800ciphone\u867d\u7136\u6ca1\u6709\u63d0\u4f9b\u7c7b\u4f3cjava\u4e0b\u7684synchronized\u5173\u952e\u5b57\uff0c\u4f46\u63d0\u4f9b\u4e86NSCondition\u5bf9\u8c61\u63a5\u53e3\u3002\u67e5\u770bNSCondition\u7684\u63a5\u53e3\u8bf4\u660e\u53ef\u4ee5\u770b\u51fa\uff0cNSCondition\u662fiphone\u4e0b\u7684\u9501\u5bf9\u8c61\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NSCondition\u5b9e\u73b0iphone\u4e2d\u7684\u7ebf\u7a0b\u5b89\u5168\u3002\u8fd9\u662f\u6765\u6e90\u4e8e\u7f51\u4e0a\u7684\u4e00\u4e2a\u4f8b\u5b50\uff1a<br \/>\nSellTicketsAppDelegate.h \u6587\u4ef6<\/p>\n<div>\n<div>\n<pre>\/\/ SellTicketsAppDelegate.h\nimport &lt;UIKit\/UIKit.h&gt;\n\u00a0<wbr> @interface SellTicketsAppDelegate : NSObject &lt;UIApplicationDelegate&gt; { int tickets; int count; NSThread* ticketsThreadone; NSThread* ticketsThreadtwo; NSCondition* ticketsCondition; UIWindow *window; } @property (nonatomic, retain) IBOutlet UIWindow *window; @end <\/wbr><\/pre>\n<\/div>\n<\/div>\n<p>SellTicketsAppDelegate.m \u6587\u4ef6<\/p>\n<div>\n<div>\n<pre>\/\/ SellTicketsAppDelegate.m\nimport \"SellTicketsAppDelegate.h\"\n\u00a0<wbr> @implementation SellTicketsAppDelegate @synthesize window; \u00a0<wbr> - (void)applicationDidFinishLaun<wbr>ching:(UIApplication *)application { tickets = 100; count = 0; \/\/ \u9501\u5bf9\u8c61 ticketCondition = [[NSCondition alloc] init]; ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; [ticketsThreadone setName:@\"Thread-1\"]; [ticketsThreadone start]; \u00a0<wbr> \u00a0<wbr> ticketsThreadtwo = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; [ticketsThreadtwo setName:@\"Thread-2\"]; [ticketsThreadtwo start]; \/\/[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil]; \/\/ Override point for customization after application launch [window makeKeyAndVisible]; \u00a0<wbr> } \u00a0<wbr> - (void)run{ while (TRUE) { \/\/ \u4e0a\u9501 [ticketsCondition lock]; if(tickets &gt; 0){ [NSThread sleepForTimeInterval:0.5]; count = 100 - tickets; NSLog(@\"\u5f53\u524d\u7968\u6570\u662f:%d,\u552e\u51fa:%d,\u7ebf\u7a0b\u540d:%@\",tickets,count,[[NSThread currentThread] name]); tickets--; }else{ break; } [ticketsCondition unlock]; } } \u00a0<wbr> - (void)dealloc { [ticketsThreadone release]; [ticketsThreadtwo release]; [ticketsCondition release]; [window release]; [super dealloc]; } @end <\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/pre>\n<\/div>\n<\/div>\n<p><strong>\u4e09\u3001\u7ebf\u7a0b\u7684\u4ea4\u4e92<\/strong><br \/>\n\u7ebf\u7a0b\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4e0e\u5176\u5b83\u7ebf\u7a0b\u8fdb\u884c\u901a\u4fe1\uff0c\u5982\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u4fee\u6539\u754c\u9762\u7b49\u7b49\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u63a5\u53e3\uff1a<\/p>\n<div>\n<div>\n<pre>- (void)performSelectorOnMainThr<wbr>ead:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait <\/wbr><\/pre>\n<\/div>\n<\/div>\n<p>\u7531\u4e8e\u5728\u672c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u91ca\u653e\u4e00\u4e9b\u8d44\u6e90\uff0c\u5219\u9700\u8981\u4f7f\u7528NSAutoreleasePool\u6765\u8fdb\u884c\u7ba1\u7406,\u5982\uff1a<\/p>\n<div>\n<div>\n<pre>- (void)startTheBackgroundJob {\nNSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];\n\/\/ to do something in your thread job\n...\n[self performSelectorOnMainThr<wbr>ead:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO]; [pool release]; } <\/wbr><\/pre>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>IOS4 \u5df2\u7ecf\u652f\u6301\u591a\u7ebf\u7a0b\u4e86,\u6211\u7684EASYWEB\u5728\u6253\u5f00\u591a\u4e2a\u7f51\u9875\u65f6\u4f1a\u5361\u5f97\u8981\u547d,\u51b3\u5b9a\u628a\u5b83\u6539\u6210\u591a\u7ebf\u7a0b\u65b9\u5f0f\u8fdb\u884c\u52a0\u8f7d\u7f51\u9875<br \/>\nIOS4\u7684\u591a\u7ebf\u7a0b,\u57fa\u4e8eObjective-c \u76f8\u5bf9 C++ JAVA\u6765\u8bf4\u7b80\u5355\u4e0d\u5c11<\/p>\n<p>\u6280\u672f\u8981\u70b9:<br \/>\n\u4e00 \u7ebf\u7a0b\u521b\u5efa\u4e0e\u542f\u52a8<br \/>\n\u7ebf\u7a0b\u7c7b NSThread<\/p>\n<p>\u5305\u542b\u5982\u4e0b\u7ebf\u7a0b\u64cd\u4f5c\u65b9\u6cd5:<\/p>\n<p><wbr>\/\/\u8fd4\u56de\u5f53\u524d\u7ebf\u7a0b<br \/>\n+ (NSThread *)currentThread;\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/p>\n<p>\/\/ \u901a\u8fc7\u7c7b\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b<br \/>\n+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument;<\/p>\n<p>\/\/ \u5224\u65ad\u662f\u5426\u4e3a\u591a\u7ebf\u7a0b<br \/>\n+ (BOOL)isMultiThreaded;<br \/>\n&#8211; (NSMutableDictionary *)threadDictionary;<\/p>\n<p>+ (void)sleepUntilDate:(NSDate *)date;<\/p>\n<p>+ (void)sleepForTimeInterval:(NSTimeInterval)ti;<\/p>\n<p>\/\/\u00a0<wbr>\u00a0\u9000\u51fa\u7ebf\u7a0b<br \/>\n+ (void)exit;<\/wbr><\/p>\n<p>\/\/ \u7ebf\u7a0b\u5c5e\u6027\u503c<br \/>\n+ (double)threadPriority ;<br \/>\n+ (BOOL)setThreadPriority:(double)p ;<\/p>\n<p>\/\/ \u7ebf\u7a0b\u51fd\u6570\u5730\u5740<br \/>\n+ (NSArray *)callStackReturnAddresses<wbr>;<\/wbr><\/p>\n<p>\/\/ \u8bbe\u7f6e\u4e0e\u8fd4\u56de\u7ebf\u7a0b\u540d\u79f0<br \/>\n&#8211; (void)setName:(NSString *)n;<br \/>\n&#8211; (NSString *)name;<\/p>\n<p>\/\/ \u7ebf\u7a0b\u5806\u6808<br \/>\n&#8211; (NSUInteger)stackSize;<br \/>\n&#8211; (void)setStackSize:(NSUInteger)s;<\/p>\n<p>\/\/ \u5224\u65ad\u5f53\u524d\u7ebf\u7a0b\u662f\u5426\u4e3a\u4e3b\u7ebf\u7a0b<br \/>\n&#8211; (BOOL)isMainThread;<br \/>\n+ (BOOL)isMainThread;<\/p>\n<p>+ (NSThread *)mainThread;<\/p>\n<p>\/\/ \u7ebf\u7a0b\u5bf9\u8c61\u521d\u59cb\u5316\u64cd\u4f5c\u00a0<wbr>\u00a0<wbr>\u00a0(\u901a\u8fc7\u521b\u5efa\u7ebf\u7a0b\u5bf9\u8c61 ,\u9700\u8981 \u624b\u5de5\u6307\u5b9a\u7ebf\u7a0b\u51fd\u6570\u4e0e\u5404\u79cd\u5c5e\u6027)<br \/>\n&#8211; (id)init;<\/wbr><\/wbr><\/p>\n<p>\/\/ \u5728\u7ebf\u7a0b\u5bf9\u8c61\u521d\u59cb\u5316\u65f6\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b(\u6307\u5b9a\u7ebf\u7a0b\u51fd\u6570)<br \/>\n&#8211; (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;<\/p>\n<p>\/\/ \u662f\u5426\u5728\u6267\u884c<br \/>\n&#8211; (BOOL)isExecuting;<\/p>\n<p>\/\/ \u662f\u5426\u5df2\u7ecf\u7ed3\u675f\u00a0<wbr><br \/>\n&#8211; (BOOL)isFinished;<\/wbr><\/p>\n<p>\/\/ \u662f\u5426\u53d6\u6d88\u7684<br \/>\n&#8211; (BOOL)isCancelled;<\/p>\n<p>\/\/ \u53d6\u6d88\u64cd\u4f5c<br \/>\n&#8211; (void)cancel;<\/p>\n<p>\/\/ \u7ebf\u7a0b\u542f\u52a8<br \/>\n&#8211; (void)start;<\/p>\n<p>&#8211; (void)main;\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0\/\/ thread body method<br \/>\n\u63a8\u8350\u65b9\u5f0f<\/wbr><\/wbr><\/wbr><\/p>\n<p>\/\/ \u901a\u8fc7\u7c7b\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b<br \/>\n+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument;<\/p>\n<p>\/\/ \u5728\u7ebf\u7a0b\u5bf9\u8c61\u521d\u59cb\u5316\u65f6\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b(\u6307\u5b9a\u7ebf\u7a0b\u51fd\u6570)<br \/>\n&#8211; (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;<\/p>\n<p>\u4e3b\u8981\u901a\u8fc7selector:(SEL)selector \u6307\u5b9a\u4e2a\u529f\u80fd\u51fd\u6570,\u7cfb\u7edf\u4f7f\u5176\u4e0e\u4e3b\u7ebf\u7a0b\u5206\u5f00\u8fd0\u884c,\u4ee5\u8fbe\u5230\u591a\u7ebf\u7a0b\u7684\u6548\u679c.<\/p>\n<p>\u4ee5\u4e0a\u65b9\u5f0f\u521b\u5efa\u7ebf\u7a0b,\u975e\u7c7b\u65b9\u6cd5\u521b\u5efa\u9700\u8981\u8c03\u7528 start\u624d\u80fd\u8ba9\u7ebf\u7a0b\u771f\u6b63\u8fd0\u884c\u8d77\u6765.<\/p>\n<p>\u5f53\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8fd0\u884c,\u5c31\u4f1a\u51fa\u73b0\u8bbf\u95ee\u8d44\u6e90\u7684\u540c\u6b65\u95ee\u9898<\/p>\n<p>\u4e8c \u7ebf\u7a0b\u540c\u6b65\u64cd\u4f5c<\/p>\n<p>IPHONE \u4f7f\u7528NSCondition\u6765\u8fdb\u884c\u7ebf\u7a0b\u540c\u6b65,\u5b83\u662fIPHONE\u7684\u9501\u5bf9\u8c61,\u7528\u6765\u4fdd\u62a4\u5f53\u524d\u8bbf\u95ee\u7684\u8d44\u6e90.<\/p>\n<p>\u5927\u81f4\u4f7f\u7528\u65b9\u6cd5\u00a0<wbr><br \/>\nNSCondition* mYLock = [[NSCondition alloc] init];<\/wbr><\/p>\n<p>[mYLock lock]<\/p>\n<p>\u8d44\u6e90&#8230;.<\/p>\n<p>[mYLock unLock];<\/p>\n<p>[mYLock release];<\/p>\n<p>\u4e09 \u7ebf\u7a0b\u7684\u4ea4\u4e92<br \/>\n<wbr><br \/>\n\u4f7f\u7528\u7ebf\u7a0b\u5bf9\u8c61\u7684<br \/>\n&#8211; (void)performSelectorOnMainThr<wbr>ead:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait<br \/>\n\u8fdb\u884c\u4ea4\u4e92\u64cd\u4f5c<br \/>\n\u4e3b\u8981\u662f\u8c03\u7528 \u4e3b\u7ebf\u7a0b\u4e2d\u6307\u5b9a\u7684\u65b9\u6cd5\u6765\u6267\u884c\u4e00\u4e9b\u76f8\u5173\u64cd\u4f5c<\/wbr><\/wbr><\/p>\n<p>\u56db \u7ebf\u7a0b\u6c60 NSOperation<br \/>\nNSInvocationOperation\u662f NSOperation\u7684\u5b50\u7c7b \u5177\u4f53\u4f7f\u7528\u4ee3\u7801<\/p>\n<p>\/\/ \u5efa\u7acb\u4e00\u4e2a\u64cd\u4f5c\u5bf9\u8c61\u00a0<wbr><br \/>\nNSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(myTaskMethod:) object:data];<\/wbr><\/p>\n<p>\/\/ \u5c06\u64cd\u4f5c\u5bf9\u8c61 \u52a0\u5230\u7cfb\u7edf\u5df2\u7ecf\u7684\u64cd\u4f5c\u5bf9\u5217\u91cc, \u8fd9\u65f6\u5019 myTaskMethod\u4ee5\u4e00\u4e2a\u7ebf\u7a0b\u7684\u65b9\u5f0f\u4e0e\u4e3b\u7ebf\u7a0b\u5206\u5f00\u6267\u884c.<br \/>\n[[MyAppDelegate sharedOperationQueue] addOperation:theOp];<\/p>\n<p>\/\/ \u8fd9\u4e2a\u662f\u771f\u6b63\u8fd0\u884c\u5728\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u7684\u201c\u65b9\u6cd5\u201d<br \/>\n&#8211; (void)myTaskMethod:(id)data<br \/>\n{<br \/>\n<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0\/\/ Perform the task.<br \/>\n}<\/wbr><\/wbr><\/wbr><\/p>\n<p>\u4ee5\u4e0a\u662f\u4f7f\u7528\u7cfb\u7edf\u64cd\u4f5c\u5bf9\u5217,\u53ef\u4ee5\u4f7f\u7528 NSOperationQueue\u521b\u5efa\u81ea\u5df1\u7684\u7ebf\u7a0b\u5bf9\u5217<\/p>\n<p>NSOperationQueue *operationQueue;\u00a0<wbr><br \/>\n<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0operationQueue = [[NSOperationQueue alloc] init]; \/\/\u521d\u59cb\u5316\u64cd\u4f5c\u961f\u5217<br \/>\n<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0[operationQueue setMaxConcurrentOperatio<wbr>nCount:n]; \/\/ \u53ef\u4ee5\u8bbe\u7f6e\u961f\u5217\u7684\u4e2a\u6570<br \/>\n<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0<wbr>\u00a0[operationQueue addOperation:otherOper];<\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/p>\n<p>\u7ebf\u7a0b\u521b\u5efa\u4e0e\u64a4\u9500\u9075\u5faa OC\u7684\u5185\u5b58\u7ba1\u7406\u89c4\u5219.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u7ebf\u7a0b\u521b\u5efa\u4e0e\u542f\u52a8 \u7ebf\u7a0b\u521b\u5efa\u4e3b\u8981\u6709\u4e8c\u79cd\u65b9\u5f0f\uff1a &#8211; (id)init; \/\/ designated initia &#8230; <a title=\"iphone \u7ebf\u7a0b NSCondition NSThread\" class=\"read-more\" href=\"https:\/\/www.sunyuping.com\/index.php\/2012\/08\/13\/iphone-%e7%ba%bf%e7%a8%8b-nscondition-nsthread\/\" aria-label=\"\u9605\u8bfb iphone \u7ebf\u7a0b NSCondition NSThread\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-2"],"_links":{"self":[{"href":"https:\/\/www.sunyuping.com\/index.php\/wp-json\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sunyuping.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sunyuping.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sunyuping.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sunyuping.com\/index.php\/wp-json\/wp\/v2\/comments?post=35"}],"version-history":[{"count":0,"href":"https:\/\/www.sunyuping.com\/index.php\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sunyuping.com\/index.php\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sunyuping.com\/index.php\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sunyuping.com\/index.php\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}