vb.net barcode printing 16: Concurrency in Java

Generation Data Matrix ECC200 in Java 16: Concurrency

CHAPTER 16: Concurrency
Making Data Matrix In Java
Using Barcode encoder for Android Control to generate, create ECC200 image in Android applications.
www.OnBarcode.com
EAN-13 Supplement 5 Encoder In Java
Using Barcode creation for Android Control to generate, create EAN13 image in Android applications.
www.OnBarcode.com
[self operationForSelector:@selector(processData)]; NSOperation *calculateFirst = [self operationForSelector:@selector(calculateFirstResult)]; NSOperation *calculateSecond = [self operationForSelector:@selector(calculateSecondResult)]; NSOperation *show = [self operationForSelector:@selector(finishWorking)];
QR Code ISO/IEC18004 Printer In Java
Using Barcode creator for Android Control to generate, create QR Code 2d barcode image in Android applications.
www.OnBarcode.com
Printing Barcode In Java
Using Barcode creator for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
Ah, isn t that better To format things for this book, each operation creation takes two lines, but if you re editing in a window that s just a little bit wider, each of those will fit nicely on a single line.
Data Matrix 2d Barcode Drawer In Java
Using Barcode generation for Android Control to generate, create Data Matrix image in Android applications.
www.OnBarcode.com
Making Barcode In Java
Using Barcode creator for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
Demanding the Main Thread
GS1 - 12 Encoder In Java
Using Barcode creator for Android Control to generate, create UPC Code image in Android applications.
www.OnBarcode.com
Make ANSI/AIM ITF 25 In Java
Using Barcode maker for Android Control to generate, create ITF image in Android applications.
www.OnBarcode.com
Now, we re still not quite ready to roll. Remember that earlier in this chapter, we mentioned that the AppKit classes (such as all the window and view classes) are generally not thread-safe. All access to them should be performed exclusively on the main thread. However, we have this finishWorking method that runs in an operation, and will in all likelihood be run on some other thread when its time comes! To get around this problem, we ll drop down to the NSThread class, which provides facilities for checking which thread we re running on, and for calling methods on background threads or the main thread itself. Using NSThread, we can check at the start of finishWorking to see if we re currently running on the main thread, and if not, do so! Here s an initial implementation:
Draw Data Matrix In Java
Using Barcode drawer for Java Control to generate, create Data Matrix image in Java applications.
www.OnBarcode.com
Data Matrix Creation In Java
Using Barcode generator for Android Control to generate, create DataMatrix image in Android applications.
www.OnBarcode.com
- (void)finishWorking { if(![NSThread isMainThread]) { [self performSelectorOnMainThread:@selector(finishWorking) withObject:nil waitUntilDone:NO]; return; } NSString *resultsSummary = [NSString stringWithFormat: @"First: [%@]\nSecond: [%@]", self.firstResult, self.secondResult]; [resultsTextView setString:resultsSummary]; NSDate *endTime = [NSDate date]; NSLog(@"Completed in %f seconds", [endTime timeIntervalSinceDate:startTime]); self.isWorking = NO; }
Painting Barcode In Objective-C
Using Barcode printer for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Generating EAN13 In Visual Studio .NET
Using Barcode printer for Reporting Service Control to generate, create EAN13 image in Reporting Service applications.
www.OnBarcode.com
Our method now first checks to see if it s running on the main thread already. If not, it relaunches itself on the main thread, and returns so that the rest of the method doesn t go on executing on the non-main thread. Then, when the method is relaunched on the main thread, it continues on through and does its work. Once you get started working with operation queues, you may find yourself need this kind of functionality in a lot of your worker methods, forcing them to run on the main thread in order to update the GUI. What we ve just shown can be improved a bit by putting it into a C preprocessor macro. Because this is essentially adding functionality
GTIN - 128 Generation In Java
Using Barcode maker for BIRT reports Control to generate, create GS1 128 image in Eclipse BIRT applications.
www.OnBarcode.com
Encode PDF417 In Java
Using Barcode creation for Eclipse BIRT Control to generate, create PDF-417 2d barcode image in BIRT applications.
www.OnBarcode.com
CHAPTER 16: Concurrency
Printing Barcode In VS .NET
Using Barcode maker for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Data Matrix Printer In Objective-C
Using Barcode creator for iPad Control to generate, create Data Matrix ECC200 image in iPad applications.
www.OnBarcode.com
related to another Foundation class (NSThread), let s go ahead and put this into the FoundationAdditions.h file we created earlier:
Paint Barcode In Objective-C
Using Barcode generation for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Printing Barcode In Java
Using Barcode drawer for BIRT reports Control to generate, create Barcode image in BIRT reports applications.
www.OnBarcode.com
#define DISPATCH_ON_MAIN_THREAD if(![NSThread isMainThread]) { \ [self performSelectorOnMainThread:_cmd withObject:nil \ waitUntilDone:NO]; \ return; }
Create Code 128B In .NET Framework
Using Barcode creation for Reporting Service Control to generate, create Code 128A image in Reporting Service applications.
www.OnBarcode.com
GS1-128 Generation In Visual C#.NET
Using Barcode generation for Visual Studio .NET Control to generate, create EAN128 image in .NET applications.
www.OnBarcode.com
There are two tricks to point out in that snippet. The first is that each line (except for the last) ends with a backslash. The appearance of a single backslash in a C preprocessor macro (in case you re unfamiliar with it) causes the preprocessor to ignore the next character it finds. In this case, putting it at the end of the line makes it ignore the following carriage return, so that it goes on to interpret the next line as part of the macro (make sure you don t have any empty space at the end of the line, otherwise this won t work). The second trick is that instead of naming a selector as we did initially, we re using a special variable, available to the preprocessor at compile-time, called _cmd. The _cmd variable always contains the selector of the current method. With that in place, we can force main thread execution of any of our worker methods by simply starting them off like this:
- (void)finishWorking { DISPATCH_ON_MAIN_THREAD NSString *resultsSummary = [NSString stringWithFormat: @"First: [%@]\nSecond: [%@]", self.firstResult, self.secondResult]; [resultsTextView setString:resultsSummary]; NSDate *endTime = [NSDate date]; NSLog(@"Completed in %f seconds", [endTime timeIntervalSinceDate:startTime]); self.isWorking = NO; }
Now you should be able to Build & Run your app, and you ll notice something perhaps a little different after pressing the Start button: the button immediately reverts back to its non-clicked state, and the menus still work. After about seven seconds (down from the ten seconds the first version used, since some of our work-methods are running at the same time now), the output appears in the text view. That s all well and good, but we can easily make things even more responsive by using Cocoa Bindings and the isWorking property! Open up MainMenu.xib in Interface Builder again, and use the Library window to find a circular progress indicator. Then add it to your window as shown in Figure 16 2.
Copyright © OnBarcode.com . All rights reserved.