Multithreading in various Programming Language It is difficult to implement in many languages. Although objective-C originated from C, its multi-threaded programming is quite simple and can be comparable to Java. This article Article I will briefly explain the multi-thread programming in iPhone from four aspects: thread creation and startup, thread synchronization and lock, thread interaction, and thread pool. I. Thread creation and startup There are two main ways to create a thread: - ( ID ) Init ; // Designated initializer - ( ID ) Initwithtarget : ( ID ) Target Selector : ( Sel ) Selector object : ( ID ) Argument ; Of course, there is another special kind, that is, the so-called convenient method, which can directly generate a thread and start it, without being responsible for thread cleaning. The interface for this method is: + (Void)Detachnewthreadselector:(Sel)Aselector totarget:(ID)Atarget withobject:(ID)Anargument
After the first two methods are created, the mobile phone needs to be started. The start method is as follows: - (Void)Start;
Ii. Synchronization and lock of threads To describe the synchronization and lock of threads, the best example may be the ticket sales system with multiple windows at the same time. We know that synchronized is used for synchronization in Java. Although the iPhone does not provide a keyword similar to synchronized in Java, it provides an nscondition object interface. View the nscondition interface description. We can see that nscondition is the lock object under the iPhone, so we can use nscondition to implement thread security in the iPhone. This is an example from the Internet: Sellticketsappdelegate. h file // Sellticketsappdelegate. h Import<Uikit/Uikit.H> @ Interface sellticketsappdelegate : Nsobject < Uiapplicationdelegate > { Int Tickets ; Int Count ; Nsthread * Ticketsthreadone ; Nsthread * Ticketsthreadtwo ; Nscondition * Ticketscondition ; Uiwindow * Window ; } @ Property ( Nonatomic , Retain ) Iboutlet uiwindow * Window ; @ End
Sellticketsappdelegate. M file // Sellticketsappdelegate. m 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 detachnewthreadselectorselector (run) totarget: Self withobject: Nil]; // Override point for customization after application launch [ Window makekeyandvisible; } - ( Void ) Run { While ( True ) { // Lock [ Ticketscondition lock ; If ( Tickets > 0 ) { [ Nsthread sleepfortimeinterval : 0.5 ; Count = 100 - Tickets ; Nslog ( @"Current ticket count: % 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
3. Thread Interaction When a thread is running, it may need to communicate with other threads, such as modifying the interface in the main thread. You can use the following interface: - (Void)Performselecdomainmainthread:(Sel)Aselector withobject:(ID)Arg waituntildone:(Bool)Wait
Because some resources may need to be released during this process, you need to use the NSAID utoreleasepool for management, such: - ( Void ) Startthebackgroundjob{ NSAID utoreleasepool * Pool = [ [ NSAID utoreleasepool alloc init ; // To do something in your thread job ... [ Self defined mselecdomainmainthread : @ Selector ( Makemyprogressbarmoving ) Withobject : Nil waituntildone : No ; [ Pool release ; }
|