vb.net barcode printing 16: Concurrency in Java

Generation DataMatrix in Java 16: Concurrency

CHAPTER 16: Concurrency
Data Matrix ECC200 Creator In Java
Using Barcode creation for Android Control to generate, create DataMatrix image in Android applications.
www.OnBarcode.com
Barcode Generation In Java
Using Barcode generator for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
iPhone), and also show you how it can be done using the new Grand Central Dispatch, available only in Snow Leopard.
Code 128 Code Set A Generator In Java
Using Barcode maker for Android Control to generate, create Code 128A image in Android applications.
www.OnBarcode.com
PDF 417 Drawer In Java
Using Barcode encoder for Android Control to generate, create PDF-417 2d barcode image in Android applications.
www.OnBarcode.com
SlowWorker
DataMatrix Generator In Java
Using Barcode creation for Android Control to generate, create ECC200 image in Android applications.
www.OnBarcode.com
Encode Code-39 In Java
Using Barcode creation for Android Control to generate, create Code39 image in Android applications.
www.OnBarcode.com
As a platform for demonstrating these concurrency options, we ll create a simple application called SlowWorker, which simulates doing some long-running operations such as fetching data from a server, and performing some calculations. This app presents the user with a button to initiate some work, and shows the results in a text view when it s done (see Figure 16 1).
Generate UPC - 13 In Java
Using Barcode generation for Android Control to generate, create EAN13 image in Android applications.
www.OnBarcode.com
Creating ITF In Java
Using Barcode creation for Android Control to generate, create ITF image in Android applications.
www.OnBarcode.com
Figure 16 1. The SlowWorker in action (inaction ).
Paint ECC200 In Objective-C
Using Barcode generator for iPad Control to generate, create DataMatrix image in iPad applications.
www.OnBarcode.com
Make Data Matrix In Visual Studio .NET
Using Barcode creation for Visual Studio .NET Control to generate, create Data Matrix image in Visual Studio .NET applications.
www.OnBarcode.com
Start by making a new Cocoa application (no Core Data or Document support is necessary) in Xcode, and doing the usual steps of enabling garbage collection, and creating a SlowWorkerAppDelegate class and adding it to the MainMenu.xib if necessary (say, if you re running Leopard, and Xcode didn t create it for you). Put the following code into SlowWorkerAppDelegate.h:
Scanning Code39 In .NET Framework
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Scan EAN13 In Visual C#.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
#import <Cocoa/Cocoa.h> @interface SlowWorkerAppDelegate : NSObject {
Create EAN / UCC - 13 In VS .NET
Using Barcode maker for ASP.NET Control to generate, create EAN 13 image in ASP.NET applications.
www.OnBarcode.com
Barcode Encoder In .NET
Using Barcode creation for .NET framework Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
CHAPTER 16: Concurrency
Paint Data Matrix 2d Barcode In Visual C#
Using Barcode generator for .NET framework Control to generate, create Data Matrix image in Visual Studio .NET applications.
www.OnBarcode.com
Data Matrix 2d Barcode Printer In VB.NET
Using Barcode generator for Visual Studio .NET Control to generate, create Data Matrix ECC200 image in .NET framework applications.
www.OnBarcode.com
IBOutlet NSButton *startButton; IBOutlet NSTextView *resultsTextView; } - (IBAction)doWork:(id)sender; @end
Quick Response Code Drawer In None
Using Barcode generator for Font Control to generate, create QR Code JIS X 0510 image in Font applications.
www.OnBarcode.com
Painting USS Code 128 In C#
Using Barcode maker for VS .NET Control to generate, create Code 128 Code Set C image in .NET applications.
www.OnBarcode.com
This simply defines a couple of outlets to the two objects visible in our GUI, and an action method to be triggered by the button. Now enter the following code for SlowWorkerAppDelegate.m:
Encode ANSI/AIM Code 128 In Visual Basic .NET
Using Barcode drawer for .NET framework Control to generate, create Code128 image in .NET framework applications.
www.OnBarcode.com
Barcode Reader In .NET Framework
Using Barcode decoder for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
#import "SlowWorkerAppDelegate.h" @implementation SlowWorkerAppDelegate - (NSString *)fetchSomethingFromServer { sleep(1); return @"Hi there"; } - (NSString *)processData:(NSString *)data { sleep(2); return [data uppercaseString]; } - (NSString *)calculateFirstResult:(NSString *)data { sleep(3); return [NSString stringWithFormat:@"Number of chars: %d", [data length]]; } - (NSString *)calculateSecondResult:(NSString *)data { sleep(4); return [data stringByReplacingOccurrencesOfString:@"E" withString:@"e"]; } - (IBAction)doWork:(id)sender { NSDate *startTime = [NSDate date]; NSString *fetchedData = [self fetchSomethingFromServer]; NSString *processed = [self processData:fetchedData]; NSString *firstResult = [self calculateFirstResult:processed]; NSString *secondResult = [self calculateSecondResult:processed]; NSString *resultsSummary = [NSString stringWithFormat: @"First: [%@]\nSecond: [%@]", firstResult, secondResult]; [resultsTextView setString:resultsSummary]; NSDate *endTime = [NSDate date]; NSLog(@"Completed in %f seconds", [endTime timeIntervalSinceDate:startTime]); } @end
As you can see, the work of this class (such as it is) is split up into a number of small chunks. This code is just meant to simulate some slow activities, and none of those methods really does anything time consuming at all, so, to make it interesting, each method contains a call to the sleep() function, which simply makes the program (specifically, the thread from which the function is called) effectively pause and do nothing at all for the given number of seconds. The doWork: method also contains code at the beginning and end to calculate the amount of time it took for all the work to be done.
CHAPTER 16: Concurrency
Now, open up MainMenu.xib, and put an NSButton and an NSTextView into the empty window, laying things out as shown in Figure 16 1. Connect the app delegate s two outlets to the relevant controls, and connect the button s action back to the app delegate s doWork: method. While you re in there, configure the NSTextView a bit, deleting the example text from the view, and turning off the Editable checkbox in the Attributes Inspector. Now save your work, and hit Build & Run in Xcode. Your app should start, and clicking the button will make it work for about ten seconds (the sum of all those sleep amounts) before showing you the results. About five or six seconds in, you ll see that the mouse cursor changes to the spinning-disk cursor, and it stays that way until the work is complete. Also, during the entire time, the application s menu is unresponsive, along with the window controls. In fact, the only way you can interact with your application at all, besides killing it with Mac OS X s Force Quit window, is to move its window around, because the OS itself handles that. This is exactly the state of affairs we want to avoid! In this particular case it s not too bad, because the application appears to be hung for just a few seconds, but if your app regularly beachballs this way for much longer than that, you ll end up with some unhappy users and maybe even some ex-users!
Copyright © OnBarcode.com . All rights reserved.