摘要:You should initialize most fields normally, not lazily. If you must initialize a field lazily in order to achieve your performance goals, or to break a harmful initialization circularity, then use the appropriate lazy initialization technique. For instance fields, it is the double-check idiom; for static fields, the lazy initialization holder class idiom. For instance fields that can tolerate repeated initialization, you may also consider the single-check idiom. 阅读全文
Effective Java 71 Use lazy initialization judiciously
2014-05-05 08:40 by 小郝(Kaibo Hao), 544 阅读, 0 推荐, 收藏, 编辑Effective Java 70 Document thread safety
2014-05-04 08:54 by 小郝(Kaibo Hao), 512 阅读, 0 推荐, 收藏, 编辑
摘要:Every class should clearly document its thread safety properties with a carefully worded prose description or a thread safety annotation. The synchronized modifier plays no part in this documentation. Conditionally thread-safe classes must document which method invocation sequences require external synchronization, and which lock to acquire when executing these sequences. If you write an unconditionally thread-safe class, consider using a private lock object in place of synchronized methods. Thi 阅读全文
Effective Java 69 Prefer concurrency utilities to wait and notify
2014-05-03 21:13 by 小郝(Kaibo Hao), 536 阅读, 0 推荐, 收藏, 编辑
摘要:using wait and notify directly is like programming in "concurrency assembly language," as compared to the higher-level language provided by java.util.concurrent. There is seldom, if ever, a reason to use wait and notify in new code. If you maintain code that uses wait and notify, make sure that it always invokes wait from within a while loop using the standard idiom. The notifyAll method should generally be used in preference to notify. If notify is used, great care must be taken to ensure liven 阅读全文
Effective Java 68 Prefer executors and tasks to threads
2014-05-02 23:39 by 小郝(Kaibo Hao), 597 阅读, 0 推荐, 收藏, 编辑
摘要:The general mechanism for executing tasks is the executor service. If you think in terms of tasks and let an executor service execute them for you, you gain great flexibility in terms of selecting appropriate execution policies. In essence, the Executor Framework does for execution what the Collections Framework did for aggregation. 阅读全文
Effective Java 67 Avoid excessive synchronization
2014-05-01 22:17 by 小郝(Kaibo Hao), 581 阅读, 0 推荐, 收藏, 编辑
摘要:To avoid deadlock and data corruption, never call an alien method from within a synchronized region. More generally, try to limit the amount of work that you do from within synchronized regions. When you are designing a mutable class, think about whether it should do its own synchronization. In the modern multicore era, it is more important than ever not to synchronize excessively. Synchronize your class internally only if there is a good reason to do so, and document your decision clearly (Item 阅读全文
Effective Java 66 Synchronize access to shared mutable data
2014-04-30 23:53 by 小郝(Kaibo Hao), 478 阅读, 0 推荐, 收藏, 编辑
摘要:When multiple threads share mutable data, each thread that reads or writes the data must perform synchronization. Without synchronization, there is no guarantee that one thread’s changes will be visible to another. The penalties for failing to synchronize shared mutable data are liveness and safety failures. If you need only inter-thread communication, and not mutual exclusion, the volatile modifier is an acceptable form of synchronization, but it can be tricky to use correctly. 阅读全文
Effective Java 65 Don't ignore exceptions
2014-04-29 21:12 by 小郝(Kaibo Hao), 471 阅读, 0 推荐, 收藏, 编辑
摘要:The advice in this item applies equally to checked and unchecked exceptions. Whether an exception represents a predictable exceptional condition or a programming error, ignoring it with an empty catch block will result in a program that continues silently in the face of error. The program might then fail at an arbitrary time in the future, at a point in the code that bears no apparent relation to the source of the problem. Properly handling an exception can avert failure entirely. Merely letting 阅读全文
Effective Java 64 Strive for failure atomicity
2014-04-28 06:02 by 小郝(Kaibo Hao), 589 阅读, 0 推荐, 收藏, 编辑
摘要:Any generated exception that is part of a method's specification should leave the object in the same state it was in prior to the method invocation. Where this rule is violated, the API documentation should clearly indicate what state the object will be left in. 阅读全文
Effective Java 63 Include failure-capture information in detail message
2014-04-27 20:46 by 小郝(Kaibo Hao), 519 阅读, 0 推荐, 收藏, 编辑
摘要:As suggested in Item 58, it may be appropriate for an exception to provide accessor methods for its failure-capture information (lowerBound, upperBound, and inde xin the above example). It is more important to provide such accessor methods on checked exceptions than on unchecked exceptions, because the failure-capture information could be useful in 阅读全文
Effective Java 62 Document all exceptions thrown by each method
2014-04-26 21:03 by 小郝(Kaibo Hao), 339 阅读, 0 推荐, 收藏, 编辑
摘要:Document every exception that can be thrown by each method that you write. This is true for unchecked as well as checked exceptions, and for abstract as well as concrete methods. Provide individual throws clauses for each checked exception and do not provide throws clauses for unchecked exceptions. If you fail to document the exceptions that your methods can throw, it will be difficult or impossible for others to make effective use of your classes and interfaces. 阅读全文
Effective Java 61 Throw exceptions appropriate to the abstraction
2014-04-25 22:38 by 小郝(Kaibo Hao), 366 阅读, 0 推荐, 收藏, 编辑
摘要:If it isn't feasible to prevent or to handle exceptions from lower layers, use exception translation, unless the lower-level method happens to guarantee that all of its exceptions are appropriate to the higher level. Chaining provides the best of both worlds: it allows you to throw an appropriate higher-level exception, while capturing the underlying cause for failure analysis (Item 63). 阅读全文
Effective Java 60 Favor the use of standard exceptions
2014-04-24 17:11 by 小郝(Kaibo Hao), 387 阅读, 0 推荐, 收藏, 编辑
摘要:Reuse must be based on semantics, not just on name. Also, feel free to subclass an existing exception if you want to add a bit more failure-capture information (Item 63). 阅读全文
Effective Java 59 Avoid unnecessary use of checked exceptions
2014-04-23 17:50 by 小郝(Kaibo Hao), 421 阅读, 0 推荐, 收藏, 编辑
摘要:If the programmer using the API can do no better an unchecked exception would be more appropriate. The checked nature of the exception provides no benefit to the programmer, but it requires effort and complicates programs.Turning a checked exception into an unchecked exception is to break the method that throws the exception into two methods, the first of which returns a boolean that indicates whether the exception would be thrown. 阅读全文
Effective Java 58 Use checked exceptions for recoverable conditions and runtime exceptions for programming errors
2014-04-22 08:46 by 小郝(Kaibo Hao), 624 阅读, 0 推荐, 收藏, 编辑
摘要:Use checked exceptions for recoverable conditions and runtime exceptions for programming errors. Of course, the situation is not always black and white. 阅读全文
Effective Java 57 Use exceptions only for exceptional conditions
2014-04-21 23:57 by 小郝(Kaibo Hao), 406 阅读, 0 推荐, 收藏, 编辑
摘要:Exceptions are designed for use in exceptional conditions. Don't use them for ordinary control flow, and don't write APIs that force others to do so. 阅读全文
Effective Java 56 Adhere to generally accepted naming conventions
2014-04-20 10:08 by 小郝(Kaibo Hao), 374 阅读, 0 推荐, 收藏, 编辑
摘要:Internalize the standard naming conventions and learn to use them as second nature. The typographical conventions are straightforward and largely unambiguous; the grammatical conventions are more complex and looser. To quote from The Java Language Specification [JLS, 6.8], "These conventions should not be followed slavishly if long-held conventional usage dictates otherwise." Use common sense. 阅读全文
Effective Java 55 Optimize judiciously
2014-04-18 22:46 by 小郝(Kaibo Hao), 415 阅读, 0 推荐, 收藏, 编辑
摘要:Do not strive to write fast programs—strive to write good ones; speed will follow. Do think about performance issues while you're designing systems and especially while you're designing APIs, wire-level protocols, and persistent data formats. When you've finished building the system, measure its performance. If it's fast enough, you're done. If not, locate the source of the problems with the aid of a profiler, and go to work optimizing the relevant parts of the system. The first step is to exami 阅读全文
Effective Java 54 Use native methods judiciously
2014-04-17 20:29 by 小郝(Kaibo Hao), 391 阅读, 0 推荐, 收藏, 编辑
摘要:Think twice before using native methods. Rarely, if ever, use them for improved performance. If you must use native methods to access low-level resources or legacy libraries, use as little native code as possible and test it thoroughly. A single bug in the native code can corrupt your entire application. 阅读全文
Effective Java 53 Prefer interfaces to reflection
2014-04-16 13:28 by 小郝(Kaibo Hao), 469 阅读, 0 推荐, 收藏, 编辑
摘要:Reflection is a powerful facility that is required for certain sophisticated system programming tasks, but it has many disadvantages. If you are writing a program that has to work with classes unknown at compile time, you should, if at all possible, use reflection only to instantiate objects, and access the objects using some interface or superclass that is known at compile time. 阅读全文
Effective Java 52 Refer to objects by their interfaces
2014-04-15 22:01 by 小郝(Kaibo Hao), 355 阅读, 0 推荐, 收藏, 编辑
摘要:In practice, it depends whether a given object has an appropriate interface. If it does, your program will be more flexible if you use the interface to refer to the object; if not, just use the least specific class in the class hierarchy that provides the required functionality. 阅读全文