
Reply:
iphone multithreading Thread (iphone development started 1) Collection
The following is a little early in the development of the information collected, simple and practical, the couple hope to have the help, are collected by the network, to the original source is unknown, if violated your rights, please inform, I will promptly remove the relevant content.
Multi-threaded multi-threaded programming of NSInvocationOperation prevent the main thread is blocked, increase operational efficiency, and so the best way. The original multi-threaded approach, there are many problems, including thread locking and so on. In Cocoa, Apple offers NSOperation this class, provides an excellent multi-threaded programming. The introduction NSOperation subset, NSInvocationOperation simple method: @ implementation MyCustomClass - (void) launchTaskWithData: (id) data { / / Create a NSInvocationOperation object and initialize the method / / Here, the value of the parameter after the selector is you wanted methods (functions, Method) / / Here, the value object after trying to pass to the data in front of methods NSInvocationOperation * theOp = [[NSInvocationOperation alloc] initWithTarget running in another thread: self selector: @ selector (myTaskMethod: ) object: data]; / / The following will operate our established "Operation" was added to the local program shared queue (method will be executed immediately after joining) / / more often are created by our own "action" Queue [[MyAppDelegate sharedOperationQueue] addOperation: theOp]; } / / this is actually run in a separate thread "approach" - (void) myTaskMethod: (id) data { / / Perform the task } @ end a NSOperationQueue operations queue. equivalent to a thread manager, rather than a thread. Because you can set within this thread manager can run in parallel on the number of threads and so on. The following operation is to create and initialize a queue:
@ Interface MyViewController: UIViewController { NSOperationQueue * operationQueue; / / declare the queue in the header file} @ end @ implementation MyViewController - (id) init { self = [super init]; if (self) { operationQueue = [[NSOperationQueue alloc] init]; / / initialize operation queue [operationQueue setMaxConcurrentOperationCount: 1]; / / Here define the queue run only one thread while / / This queue can has spent } return self; } - [operationQueue release (void) dealloc { ]; / / As Alan always said that we are good citizens program, you need to release the memory! [Super dealloc]; } @ end after a brief introduction, we can find this method is very simple. Many times we use multiple threads just to prevent clogging of the main thread, and NSInvocationOperation is the most simple multi-threaded programming, iPhone programming is often used.
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 1 In the main thread to add a loading screen ...... 2 { 3 [window addSubview: view_loading]; 4 [NSThread detachNewThreadSelector: @ selector (init_backup :) toTarget: self withObject: nil]; 5} by performSelectorOhMainThread update UI elements, such as setting progress bar and so on. Finally, to eliminate loading screen, load master View. 7 - (void) init_backup: (id) sender 8 { 9 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 10 11 / / ... 12 int i = status; 13 [self performSelectorOnMainThread: @ selector (show_loading :) withObject: [NSNumber numberWithInt: i] waitUntil Done: NO]; 14 15 [view_loading removeFromSuperview]; 16 [window addSubview: tabcontroller_main.view]; 17 [pool release]; 18}
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
Use iphone multithreading and thread synchronization from the definition of the interface can know, NSThread and most of the iphone interface objects, there are two ways to initialize:
A use initWithTarget: (id) target selector: (SEL) selector object: (id) argument, but need to be responsible for the retain count objects 0 release calling object methods when cleaning up objects.
Another is the use of so-called convenient method, this handy interface is a detachNewThreadSelector, this method can generate a thread and start it directly, without the need to clean up responsible for the thread.
# Import
@ Property (nonatomic, retain) IBOutlet UIWindow * window; @ end
Then add the following code in your implementation:.. / / SellTicketsAppDelegate.m / / SellTickets / / / / Created by sun dfsun2009 on 09-11-10 / / Copyright __ MyCompanyName__ 2009 All rights reserved / / # import "SellTicketsAppDelegate.h". @ implementation SellTicketsAppDelegate @ synthesize window; - (void) applicationDidFinishLaunching: (UIApplication *) application { tickets = 100; count = 0; / / lock object ticketCondition = [[NSCondition alloc] init]; ticketsThreadone = [[NSThread alloc] initWithTarget: self selector: @ selector (run) object: nil]; [ticketsThreadone setName: @ "Thread-1"]; [ticketsThreadone start]; 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 ]; } - (void) run { while (TRUE) { / / locked [ticketsCondition lock]; if (tickets> 0) { [NSThread sleepForTimeInterval: 0.5]; count = 100 - tickets; NSLog (@ "The current number of votes is:% d, sold:% d, thread name:% @ ", tickets, count, [[NSThread currentThread] name]); tickets -; } else { break; } [ticketsCondition unlock]; } } - ( void) dealloc { [ticketsThreadone release]; [ticketsThreadtwo release]; [ticketsCondition release]; [window release]; [super dealloc]; } @ end
-------------------------------------------------- ----------------------------------- / / define # import
@ Interface ThreadSyncSampleViewController: UIViewController { int _threadCount; NSCondition * _myCondition; }
@ End
/ / Implementation file as follows:
# Import "ThreadSyncSampleViewController.h"
@ Implementation ThreadSyncSampleViewController
/ * / / The designated initializer Override to perform setup that is required before the view is loaded - (id) initWithNibName:.. (NSString *) nibNameOrNil bundle: (NSBundle *) nibBundleOrNil { if (self = [super initWithNibName: nibNameOrNil bundle : nibBundleOrNil]) { / / Custom initialization } return self; } * /
/ * / / Implement loadView to create a view hierarchy programmatically, without using a nib -. (Void) loadView { } * /
/ / Implement viewDidLoad to do additional setup after loading the view, typically from a nib - (void) viewDidLoad { [super viewDidLoad];. / / / / _myCondition = nil; / / _myCondition = [[NSCondition alloc] init]; / / NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval: 30 target: self selector: @ selector (threadTester) userInfo: nil repeats: YES]; [timer fire]; }
- (Void) threadTester { [_myCondition lock]; _threadCount = -2; / / If there are n to wait for the thread, here set to-n [_myCondition unlock]; / / NSLog (@ ""); NSLog (@ " -------------------------------------------------- ---------------------------- "); [NSThread detachNewThreadSelector: @ selector (threadOne) toTarget: self withObject: nil]; [NSThread detachNewThreadSelector: @ selector (threadTwo) toTarget: self withObject: nil]; [NSThread detachNewThreadSelector: @ selector (threadThree) toTarget: self withObject: nil]; return; }
- (Void) threadOne { NSLog (@ "@ @ @ In thread 111111 start."); [_myCondition lock]; int n = rand ()% 5 + 1; NSLog (@ "@ @ @ Thread 111111 Will sleep% d seconds, now _threadCount is:% d ", n, _threadCount); sleep (n); / / [NSThread sleepForTimeInterval: n]; _threadCount + +; NSLog (@" @ @ @ Thread 111111 has sleep% d seconds, now _threadCount is:% d ", n, _threadCount); [_myCondition signal]; NSLog (@" @ @ @ Thread 1111111 has signaled, now _threadCount is:% d ", _threadCount); [_myCondition unlock]; NSLog (@" @ @ @ In thread one complete ");. [NSThread exit]; return; }
- (Void) threadTwo { NSLog (@ ". # # # In thread 2222222 start"); [_myCondition lock]; int n = rand ()% 5 + 1; NSLog (@ "# # # Thread 2222222 Will sleep% d seconds, now _threadCount is:% d ", n, _threadCount); sleep (n); / / [NSThread sleepForTimeInterval: n]; _threadCount + +; NSLog (@" # # # Thread 2222222 has sleep% d seconds, now _threadCount is:% d ", n, _threadCount); [_myCondition signal]; NSLog (@" # # # Thread 2222222 has signaled, now _threadCount is:% d ", _threadCount); [_myCondition unlock]; / / _threadCount + +; NSLog (@ "# # # In thread 2222222 complete."); [NSThread exit]; return; }
- (Void) threadThree { NSLog (@ "<<< In thread 333333 start."); [_myCondition lock]; while (_threadCount <0) { [_myCondition wait]; } NSLog (@ "<< ;
- (Void) didReceiveMemoryWarning { / / Releases the view if it doesn't have a superview [super didReceiveMemoryWarning]; / / Release any cached data, images, etc that aren't in use ..}
- (Void) viewDidUnload {. / / Release any retained subviews of the main view / / eg self.myOutlet = nil; }
- (Void) dealloc { [_myCondition release]; [super dealloc]; }
@ End
Their landlord to see it before I used this study
Reply:
Like I can not get too messy attachments do not know how to come here on
Reply:
Did not you ask the company also posted it, ha ha, funny it
Reply:
How to download files? ?
Reply:
Just how much `` `
Reply:
Egg hurt you!
Reply:
Here is a cross-platform download library, support for HTTP, FTP, HTTPS, multi-threaded downloads, can run on the phone, you can refer
http://www.easygeteasy.com
Reply:
No comments:
Post a Comment