vb.net print barcode labels 3: Doing Several Things at Once: Performance Enhancements with Threading in Objective-C

Generate Data Matrix ECC200 in Objective-C 3: Doing Several Things at Once: Performance Enhancements with Threading

CHAPTER 3: Doing Several Things at Once: Performance Enhancements with Threading
Draw DataMatrix In Objective-C
Using Barcode encoder for iPhone Control to generate, create Data Matrix ECC200 image in iPhone applications.
www.OnBarcode.com
Draw Barcode In Objective-C
Using Barcode maker for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Figure 3-12. Connect all thread labels to the corresponding Thread Count values.
Drawing EAN 128 In Objective-C
Using Barcode drawer for iPhone Control to generate, create UCC - 12 image in iPhone applications.
www.OnBarcode.com
ANSI/AIM Code 128 Generator In Objective-C
Using Barcode creator for iPhone Control to generate, create Code 128 Code Set B image in iPhone applications.
www.OnBarcode.com
Figure 3-13. Connect the totalCount thread to the associated label.
Paint Code 3 Of 9 In Objective-C
Using Barcode generation for iPhone Control to generate, create Code-39 image in iPhone applications.
www.OnBarcode.com
Painting QR Code ISO/IEC18004 In Objective-C
Using Barcode drawer for iPhone Control to generate, create Quick Response Code image in iPhone applications.
www.OnBarcode.com
CHAPTER 3: Doing Several Things at Once: Performance Enhancements with Threading
Create GTIN - 13 In Objective-C
Using Barcode maker for iPhone Control to generate, create EAN / UCC - 13 image in iPhone applications.
www.OnBarcode.com
EAN8 Encoder In Objective-C
Using Barcode maker for iPhone Control to generate, create EAN 8 image in iPhone applications.
www.OnBarcode.com
Next, we will update the updatedByThread outlet to the associated label, as shown in Figure 3-14. This label lists the last thread to update the Total Counts value.
ECC200 Creator In Objective-C
Using Barcode creator for iPhone Control to generate, create Data Matrix image in iPhone applications.
www.OnBarcode.com
Data Matrix 2d Barcode Decoder In Visual C#.NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Figure 3-14. Connect the totalCount thread to the associated label.
PDF417 Generator In Java
Using Barcode printer for Java Control to generate, create PDF 417 image in Java applications.
www.OnBarcode.com
Encode PDF-417 2d Barcode In None
Using Barcode maker for Microsoft Word Control to generate, create PDF 417 image in Microsoft Word applications.
www.OnBarcode.com
Finally, we will connect all the buttons with their associated events, as shown in Figure 3-15. Control-click the button and drag to the File s Owner icon. Then, select the associated event for each button to enable the button to trigger an event each time it s clicked. The event listener will then call the associated method for that event. Repeat connecting the actions and outlets for the remaining buttons and labels.
Code-128 Scanner In Visual Studio .NET
Using Barcode reader for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Recognizing Code 3 Of 9 In Visual Basic .NET
Using Barcode reader for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
CHAPTER 3: Doing Several Things at Once: Performance Enhancements with Threading
Barcode Maker In .NET Framework
Using Barcode creation for Visual Studio .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Print Code 39 Full ASCII In Objective-C
Using Barcode encoder for iPad Control to generate, create Code 39 Extended image in iPad applications.
www.OnBarcode.com
Figure 3-15. Connect the events to the associated buttons.
Create Data Matrix In Visual Basic .NET
Using Barcode drawer for .NET Control to generate, create Data Matrix ECC200 image in .NET framework applications.
www.OnBarcode.com
Draw PDF 417 In None
Using Barcode printer for Font Control to generate, create PDF417 image in Font applications.
www.OnBarcode.com
Creating a Thread
Generating PDF-417 2d Barcode In Java
Using Barcode generation for BIRT Control to generate, create PDF 417 image in BIRT applications.
www.OnBarcode.com
Generate Barcode In Java
Using Barcode drawer for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
The interface section is now described. Save the changes in Interface Builder, and let s focus on the implementation section of our ThreadingViewController. Open the ThreadingViewController.m file in Xcode. We need to complete implementing our getter and setter methods. To do so, add the code in Listing 3-2 to the implementation section of the ThreadingViewController.m file.
Listing 3-2. Completing the Getter and Setter Methods in ThreadingViewController.m
@synthesize @synthesize @synthesize @synthesize @synthesize @synthesize @synthesize @synthesize @synthesize @synthesize totalCount; thread1Label; thread2Label; thread3Label; thread4Label; button1Title; button2Title; button3Title; button4Title; updatedByThread;
CHAPTER 3: Doing Several Things at Once: Performance Enhancements with Threading
We need to look at how to implement our thread when a user wants to launch a thread by clicking a button. Clicking a Start Counting button will trigger an event that will be handled by the corresponding launchThreadX methods.
Creating a Worker Thread
It is important now to discuss how we are going to implement a worker thread and what the thread is going to do. When the thread is spawned, it increments a counter ten times, updates its display on the iPhone, sleeps after each increment, and updates the total thread counter at the end of each cycle. The cycle repeats until the user signals to kill the individual thread or kill all threads (by clicking Stop All Counting). See Figure 3-16.
performSelectorInBackground: @selector(thread1) withObject:nil
thread1
while button1On
performSelectorOnMainThread: @selector(displayThread1Counts:) withObject:myNumber waitUntilDone:YES
increment thread counts display thread counts Time sleep for 1/2 second After 10 increments, update totalThread counts by 10 exit thread1
Thread the Needle App
performSelectorOnMainThread: @selector(countThreadLoops:) withObject:myNumber waitUntilDone:NO
Application Address Space
Figure 3-16. Diagram showing when the Thread the Needle application spawns a thread and updates the view display
CHAPTER 3: Doing Several Things at Once: Performance Enhancements with Threading
With OS 10.5 (Leopard), NSObject gained an additional method called performSelectorInBackground: withObject:. This method makes it great for developers to spawn a new thread with the selector and arguments provided. You will notice, if you look at the threading APIs, that this method is just about the same as NSThread class method + detachNewThreadSelector with the added benefit that with the NSObject method, you no longer have to specify a target. Instead, you are calling the method on the intended target. When you call performSelectorInBackground: withObject:, you are essentially spawning a new thread that immediately starts executing the method in your class. This convenience method puts your application into multithreaded mode. Let s implement the code when a user clicks a Start Counting button; see Listing 3-3. Go ahead and implement the code for the remaining Start Counting buttons as well.
Listing 3-3. The launchThread1:(id)sender Method
-(IBAction)launchThread1:(id)sender { if (!button1On) { button1On = TRUE; [button1Title setTitle:@"Kill Counting 1" forState:UIControlStateNormal]; [self performSelectorInBackground:@selector(thread1) withObject:nil]; } else { button1On = FALSE; [button1Title setTitle:@"Start Counting 1" forState:UIControlStateNormal]; } }
In Listing 3-3, we first check to see what state the button is in and then we change it. Next, we change the title of the button to reflect the state. If the button is being clicked for the first time, we will launch our thread (see Listing 3-4). Implement the remaining thread logic for the other three buttons.
Listing 3-4. Launching thread1
-(void)thread1 { NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init]; NSNumber *myNumber = [NSNumber numberWithInteger:1];
CHAPTER 3: Doing Several Things at Once: Performance Enhancements with Threading
while(button1On) { for (int x=0; x<10; x++) { [self performSelectorOnMainThread:@selector (displayThread1Counts:) withObject:myNumber waitUntilDone:YES]; [NSThread sleepForTimeInterval:0.5]; } [self performSelectorOnMainThread:@selector(countThreadLoops:) withObject:myNumber waitUntilDone:NO]; } [apool release]; }
The first thing you should notice in Listing 3-4 is that we are responsible for the memory pool. That is correct! When we launch a thread, we are essentially leaving the Cocoa framework. When we do that, we are responsible for cleaning up the memory pool. If we don t, we will leak memory. We then begin a simple loop. We want to keep our worker threads busy doing something in this example, and this simple loop is an easy way to do that without creating a run loop. We will discuss run loops shortly. Our thread increments and then we see a new method call, performSelectorOnMainThread. Apple warns against calling the main thread from a worker thread, [self displayThread1Counts:], explaining that unexpected results may occur. Apple is right; I have tried it. So, to call our displayThread1Counts: method, we need to launch a thread by setting waitUntilDone to YES. This tells my current worker thread to wait until Thread1Counts is done before continuing. I did this to illustrate the waitUntilDone. I wanted to slow down the thread at this point, so I put the thread to sleep for half a second. After the loop completes, we now want to update the Total Count field on the iPhone. We now launch another thread to call the countThreadLoops: method. This time, we don t wait for the method to finish. We spawn the thread and let it operate asynchronously. Asynchronous events are those occurring independent of the calling thread or main program flow. Asynchronous just means the calling thread doesn t sit and wait for the response.
Copyright © OnBarcode.com . All rights reserved.