|
|
|
#### Thread safety
|
|
|
|
* Classes should be immutable where possible
|
|
|
|
* Classes that may be used by multiple threads must be thread-safe
|
|
|
|
* If a class is used by a single thread and is not thread-safe, add a comment
|
|
|
|
* Fields that are accessed by multiple threads must be volatile or guarded by locks
|
|
|
|
* If a field or method is guarded by a lock, add a comment
|
|
|
|
|
|
|
|
#### Visibility
|
|
|
|
* Minimise the visibility of classes, fields and methods
|
|
|
|
* Fields should be private or protected
|
|
|
|
* Fields should be final where possible
|
|
|
|
* Inner classes should be static where possible
|
|
|
|
|
|
|
|
#### Instantiation
|
|
|
|
* Don't allow `this` to escape the constructor
|
|
|
|
* Complex dependencies should be injected or constructed by factories
|
|
|
|
* Use executors rather than creating threads where possible
|
|
|
|
|
|
|
|
#### Exceptions
|
|
|
|
* Use checked exceptions rather than special return values to indicate errors
|
|
|
|
* Unchecked exceptions should only be used for programming errors
|
|
|
|
* If you catch an InterruptedException in a synchronous method, interrupt the current thread so the caller learns about the interrupt
|
|
|
|
|
|
|
|
#### Blocking
|
|
|
|
* Don't call blocking methods from the UI thread
|
|
|
|
* Don't call blocking methods from event handlers
|
|
|
|
|
|
|
|
#### Locks
|
|
|
|
* Avoid using locks where possible
|
|
|
|
* Use dedicated lock objects rather than `synchronized`
|
|
|
|
* Don't call into other classes while holding locks
|
|
|
|
* Don't start database transactions while holding locks |