- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Herb Schildt s Java Prog ramming Cookbook in Java
Herb Schildt s Java Prog ramming Cookbook Generate Data Matrix 2d Barcode In Java Using Barcode maker for Java Control to generate, create Data Matrix image in Java applications. Data Matrix Reader In Java Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications. // Start the second thread new MyThread(syncCol); try { synchronized(syncCol) { for(String str : syncCol) { Systemoutprintln("Main thread: " + str); // Let the second thread run, if it can Threadsleep(500); } } } catch(InterruptedException exc) { Systemoutprintln("Main thread interrupted"); } } } Encoding Bar Code In Java Using Barcode encoder for Java Control to generate, create bar code image in Java applications. Barcode Decoder In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. The output produced by this program is shown here
Printing DataMatrix In Visual C#.NET Using Barcode creation for .NET framework Control to generate, create DataMatrix image in VS .NET applications. Encoding ECC200 In .NET Framework Using Barcode maker for ASP.NET Control to generate, create Data Matrix ECC200 image in ASP.NET applications. Main thread: Alpha Main thread: Beta Main thread: Gamma Second thread: Alpha Second thread: Beta Second thread: Gamma Second thread: Omega Data Matrix Creation In .NET Framework Using Barcode creator for .NET framework Control to generate, create ECC200 image in .NET applications. Data Matrix Creator In VB.NET Using Barcode maker for .NET Control to generate, create DataMatrix image in .NET framework applications. Here is how the program works SyncDemo begins by creating a TreeSet called tsStr This set is then wrapped in a synchronized collection called syncCol and three elements are added to it Next, main( ) starts a second thread of execution, which is defined by MyThread The MyThread constructor is passed a reference to syncCol This thread begins by sleeping for a short period of time, which lets the main thread resume running MyThread then adds another element to the collection and then begins iterating through the collection After creating MyThread, the main thread starts iterating over the set, delaying 500 milliseconds between iterations This delay enables the second thread to run, if it can However, because syncCol is synchronized, the second thread can t run until the main thread finishes Therefore the iteration of the elements in main( ) finishes before MyThread adds another element to the set, thus avoiding a ConcurrentModificationException To prove the necessity of using a synchronized collection when multithreading, try this experiment In the example, comment out these lines: Code39 Generator In Java Using Barcode generator for Java Control to generate, create ANSI/AIM Code 39 image in Java applications. EAN13 Maker In Java Using Barcode drawer for Java Control to generate, create GTIN - 13 image in Java applications. Collection<String> syncCol = CollectionssynchronizedCollection(tsStr); UPC A Generation In Java Using Barcode creation for Java Control to generate, create GTIN - 12 image in Java applications. Create GTIN - 128 In Java Using Barcode printer for Java Control to generate, create UCC.EAN - 128 image in Java applications. Then, remove the comment from this line: UPC-E Creation In Java Using Barcode generation for Java Control to generate, create GS1 - 12 image in Java applications. Paint EAN / UCC - 14 In None Using Barcode encoder for Office Word Control to generate, create USS-128 image in Office Word applications. // Collection<String> syncCol = tsStr; Printing Matrix 2D Barcode In Visual C#.NET Using Barcode generator for VS .NET Control to generate, create Matrix 2D Barcode image in VS .NET applications. Make Barcode In Objective-C Using Barcode generator for iPad Control to generate, create bar code image in iPad applications. Now, recompile and run the program Because syncCol is no longer synchronized, both the main thread and MyThread can access it simultaneously This results in a ConcurrentModificationException when MyThread attempts to add an element to it Drawing Barcode In None Using Barcode creation for Font Control to generate, create bar code image in Font applications. EAN / UCC - 14 Maker In Objective-C Using Barcode generator for iPhone Control to generate, create EAN128 image in iPhone applications. 5: Make ECC200 In Visual C#.NET Using Barcode encoder for VS .NET Control to generate, create Data Matrix image in Visual Studio .NET applications. Create UPC Symbol In None Using Barcode printer for Software Control to generate, create UPC A image in Software applications. Wo r k i n g w i t h C o l l e c t i o n s
Options and Alternatives
In addition to synchronizedCollection( ), Collections provides several other methods that return synchronized views tailored for specific types of collections They are shown here: static <T> List<T> synchronizedList(List<T> col) static <T> Set<T> synchronizedSet(Set<T> col) static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> col) Returns a thread-safe list backed by col Returns a thread-safe set backed by col Returns a thread-safe sorted set backed by col Collections also supplies the following methods that return synchronized views of maps: static <K, V> Map<K, V> synchronizedMap(Map<K, V> map) static <K, V> SortedMap<K, V> synchronizedSortedMap( SortedMap<K, V> map) Returns a thread-safe map backed by map Returns a thread-safe sorted map backed by map Create an Immutable Collection
Key Ingredients
Classes javautilCollections Methods static <T> Collection<T> unmodifiableCollection( Collection< extends T> col) Collections supplies a set of methods that create an immutable view of a collection These methods begin with the name unmodifiable The underlying collection cannot be changed through such a view This is useful in cases in which you want to ensure that a collection will not be modified, such as when it is passed to third-party code Step-by-Step
To create an immutable view of a collection involves these steps: 1 Create a collection that will be read-only 2 Obtain an immutable view of the collection by calling the unmodifiableCollection( ) method 3 Operate on the collection through the read-only view Herb Schildt s Java Prog ramming Cookbook
Discussion
To create an immutable view of a collection, use one of the unmodifiable methods defined by the Collections class It defines four methods that return immutable views of a collection The one used by this recipe is unmodifiableCollection( ) It is shown here static <T> Collection<T> unmodi ableCollection(Collection< extends T> col) It returns a reference to a read-only view of the collection passed to col This reference can then be passed to any code that is not allowed to modify the collection Any attempt to modify the collection through this reference will result in an UnsupportedOperationException
|
|