Chapter 10:Deploying Applications and Applets 565
10.1 JAR Files 566
10.1.1 The Manifest 567
10.1.2 Executable JAR Files 568
10.1.3 Resources 569
10.1.4 Sealing 573
10.2 Java Web Start 574
10.2.1 The Sandbox 578
10.2.2 Signed Code 579
10.2.3 The JNLP API 582
10.3 Applets 591
10.3.1 A Simple Applet 591
10.3.1.1 Converting Applications to Applets 595
10.3.2 The applet HTML Tag and Its Attributes 596
10.3.3 The object Tag 599
10.3.4 Use of Parameters to Pass Information to Applets 600
10.3.5 Accessing Image and Audio Files 606
10.3.6 The Applet Context 607
10.3.6.1 Inter-Applet Communication 608
10.3.6.2 Displaying Items in the Browser 609
10.4 Storage of Application Preferences 610
10.4.1 Property Maps 611
10.4.2 The Preferences API 616
Chapter 11:Exceptions,Assertions,Logging,and Debugging 625
11.1 Dealing with Errors 626
11.1.1 The Classification of Exceptions 628
11.1.2 Declaring Checked Exceptions 630
11.1.3 How to Throw an Exception 632
11.1.4 Creating Exception Classes 634
11.2 Catching Exceptions 635
11.2.1 Catching Multiple Exceptions 637
11.2.2 Rethrowing and Chaining Exceptions 639
11.2.3 The finally Clause 640
11.2.4 The Try-with-Resources Statement 644
11.2.5 Analyzing Stack Trace Elements 646
11.3 Tips for Using Exceptions 649
11.4 Using Assertions 653
11.4.1 Assertion Enabling and Disabling 654
11.4.2 Using Assertions for Parameter Checking 655
11.4.3 Using Assertions for Documenting Assumptions 656
11.5 Logging 657
11.5.1 Basic Logging 658
11.5.2 Advanced Logging 658
11.5.3 Changing the Log Manager Configuration 661
11.5.4 Localization 662
11.5.5 Handlers 663
11.5.6 Filters 667
11.5.7 Formatters 667
11.5.8 A Logging Recipe 668
11.6 Debugging Tips 677
11.7 Tips for Troubleshooting GUI Programs 682
11.7.1 Letting the AWT Robot Do the Work 686
11.8 Using a Debugger 690
Chapter 12:Generic Programming 697
12.1 Why Generic Programming? 698
12.1.1 Who Wants to Be a Generic Programmer? 699
12.2 Defining a Simple Generic Class 700
12.3 Generic Methods 702
12.4 Bounds for Type Variables 704
12.5 Generic Code and the Virtual Machine 706
12.5.1 Translating Generic Expressions 708
12.5.2 Translating Generic Methods 708
12.5.3 Calling Legacy Code 711
12.6 Restrictions and Limitations 712
12.6.1 Type Parameters Cannot Be Instantiated with Primitive Types 712
12.6.2 Runtime Type Inquiry Only Works with Raw Types 712
12.6.3 You Cannot Create Arrays of Parameterized Types 713
12.6.4 Varargs Warnings 713
12.6.5 You Cannot Instantiate Type Variables 715
12.6.6 Type Variables Are Not Valid in Static Contexts of Generic Classes 717
12.6.7 You Cannot Throw or Catch Instances of a Generic Class 717
12.6.7.1 You Can Defeat Checked Exception Checking 718
12.6.8 Beware of Clashes after Erasure 720
12.7 Inheritance Rules for Generic Types 721
12.8 Wildcard Types 723
12.8.1 Supertype Bounds for Wildcards 725
12.8.2 Unbounded Wildcards 728
12.8.3 Wildcard Capture 728
12.9 Reflection and Generics 731
12.9.1 Using Class<T>Parameters for Type Matching 732
12.9.2 Generic Type Information in the Virtual Machine 733
Chapter 13:Collections 741
13.1 Collection Interfaces 741
13.1.1 Separating Collection Interfaces and Implementation 742
13.1.2 Collection and Iterator Interfaces in the Java Library 745
13.1.2.1 Iterators 745
13.1.2.2 Removing Elements 748
13.1.2.3 Generic Utility Methods 748
13.2 Concrete Collections 751
13.2.1 Linked Lists 752
13.2.2 Array Lists 762
13.2.3 Hash Sets 763
13.2.4 Tree Sets 767
13.2.5 Object Comparison 768
13.2.6 Queues and Deques 774
13.2.7 Priority Queues 776
13.2.8 Maps 777
13.2.9 Specialized Set and Map Classes 782
13.2.9.1 Weak Hash Maps 782
13.2.9.2 Linked Hash Sets and Maps 783
13.2.9.3 Enumeration Sets and Maps 785
13.2.9.4 Identity Hash Maps 785
13.3 The Collections Framework 787
13.3.1 Views and Wrappers 792
13.3.1.1 Lightweight Collection Wrappers 793
13.3.1.2 Subranges 794
13.3.1.3 Unmodifiable Views 794
13.3.1.4 Synchronized Views 796
13.3.1.5 Checked Views 796
13.3.1.6 A Note on Optional Operations 797
13.3.2 Bulk Operations 799
13.3.3 Converting between Collections and Arrays 800
13.4 Algorithms 801
13.4.1 Sorting and Shuffling 802
13.4.2 Binary Search 805
13.4.3 Simple Algorithms 806
13.4.4 Writing Your Own Algorithms 808
13.5 Legacy Collections 810
13.5.1 The Hashtable Class 810
13.5.2 Enumerations 810
13.5.3 Property Maps 811
13.5.4 Stacks 812
13.5.5 Bit Sets 813
13.5.5.1 The""Sieve of Eratosthenes""Benchmark 814
Chapter 14:Multithreading 819
14.1 What Are Threads? 820
14.1.1 Using Threads to Give Other Tasks a Chance 827
14.2 Interrupting Threads 833
14.3 Thread States 836
14.3.1 New Threads 836
14.3.2 Runnable Threads 836
14.3.3 Blocked and Waiting Threads 837
14.3.4 Terminated Threads 839
14.4 Thread Properties 839
14.4.1 Thread Priorities 840
14.4.2 Daemon Threads 841
14.4.3 Handlers for Uncaught Exceptions 841
14.5 Synchronization 843
14.5.1 An Example of a Race Condition 843
14.5.2 The Race Condition Explained 848
14.5.3 Lock Objects 850
14.5.4 Condition Objects 854
14.5.5 The synchronized Keyword 859
14.5.6 Synchronized Blocks 864
14.5.7 The Monitor Concept 865
14.5.8 Volatile Fields 866
14.5.9 Final Variables 867
14.5.10 Atomics 868
14.5.11 Deadlocks 868
14.5.12 Thread-Local Variables 871
14.5.13 Lock Testing and Timeouts 873
14.5.14 Read/Write Locks 874
14.5.15 Why the stop and suspend Methods Are Deprecated 875
14.6 Blocking Queues 877
14.7 Thread-Safe Collections 886
14.7.1 Efficient Maps,Sets,and Queues 886
14.7.2 Copy on Write Arrays 888
14.7.3 Older Thread-Safe Collections 888
14.8 Callables and Futures 890
14.9 Executors 895
14.9.1 Thread Pools 896
14.9.2 Scheduled Execution 900
14.9.3 Controlling Groups of Tasks 901
14.9.4 The Fork-Join Framework 902
14.10 Synchronizers 905
14.10.1 Semaphores 906
14.10.2 Countdown Latches 907
14.10.3 Barriers 907
14.10.4 Exchangers 908
14.10.5 Synchronous Queues 908
14.11 Threads and Swing 909
14.11.1 Running Time-Consuming Tasks 910
14.11.2 Using the Swing Worker 915
14.11.3 The Single-Thread Rule 923
Appendix:Java Keywords 925
Index 929