Nuances of Java development
This blog is about Java development and describes in detail the most interesting topics
All you want to know about Singleton
The singleton design pattern is one of the most inappropriately used patterns. In this article we review several implementations of a singleton that work correctly in multithreaded environment, with serialization and cloning tasks and even with reflection attacks.
Contents ✓What for you can use singleton ✓Distinction from static class ✓Lazy or eager loading singleton? ✓Eager loading singleton ✓Rough synchronization ✓Double-checked locking singleton ✓Initialization-on-demand holder idiom ✓The enum based singleton ✓Problems with serialization and deserialization ✓Problems with clone ✓Problems with reflection ✓So, why you can think about singleton as anti-pattern? ✓Conclusion What for you can use singleton The singleton pattern is used when there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point or when the single instance should be extensible by sub-classes, and clients should be able to use an extended instance without modifying their code. Distinction from static class JDK has examples of both singleton and static, on the one hand java.lang.Math is a final class with static methods, on the other hand java.lang.Runtime is a singleton class. Advantages of singleton ✓If your need to maintain state than singleton pattern is better choice than static class, because maintaining state in static class leads to bugs, especially in concurrent environment, that could lead to race conditions without adequate synchronization parallel modification by multiple threads. ✓Singleton class can be lazy loaded if its a heavy object, but static class doesn't have such advantages and always eagerly loaded. ✓With singleton, you can use inheritance and polymorphism to extend a base class, implement an interface and provide different implementations. ✓Since static methods in Java cannot be overridden, they lead to inflexibility. On the other hand, you can over... Read more
SynchronousQueue VS TransferQueue
One addition in Java 7 is an interface TransferQueue in addition already exists from Java 5 SynchronousQueue. What is the reason of new interface?
Contents ✓Firstly about TransferQueue ✓Couple words about SynchronousQueue ✓SynchronousQueue vs TransferQueue Firstly about TransferQueue Java 7 included new interface TransferQueue and corresponded implementation LinkedTransferQueue. TransferQueue extends BlockingQueue which extends Queue interface, added in Java 5. BlockingQueue is a queue, which can block Producer threads during adding items into a full queue and Consumer threads, during removing from an empty queue. Main idea of blocking queues is to cope with flood of data, which can not be processed by system for appropriate time. TransferQueue goes further, and blocks Producer threads until the items consumed by Consumer threads. New method — transfer — blocking occurs until item moves from one thread to another. There are additional methods — two forms of tryTransfer — one is blocking with time-out, other is non-blocking but transfer only if Consumer thread is waiting. Also there are a couple helper methods hasWaitingConsumer and getWaitingConsumerCount. Couple words about SynchronousQueue We remember about SynchronousQueue from Java 5, which provides queue with size 0 and quite good for transfer items between threads. This queue works on the principle «one came in — one went out». Each put operation blocks Producer thread until Consumer thread fetch element from queue and vice versa, Consumer thread is waiting until Producer thread puts element to queue. SynchronousQueue is more specialized in comparison with TransferQueue but it allows developers decide whether to use general blocking queue, or guaranteed hand-off. SynchronousQueue vs TransferQueue LinkedTransferQueue combines adv... Read more
How to use ConcurrentHashMap in Java
Before Java 1.5, if you need Map implementation, which can be safely used in multithreading Java-application, you have only Hashtable or synchronized Map, because HashMap is NOT safe.
Contents ✓Introduction ✓How ConcurrentHashMap is implemented ✓Some important features of ConcurrentHashMap ✓When you should use ConcurrentHashMap in Java Introduction ConcurrentHashMap was presented as alternative to Hashtable in Java 1.5 as part of concurrency package. With ConcurrentHashMap, you have a better choice not only if it can be safely used in the concurrent multi-threaded environment but also provides better performance than Hashtable and synchronizedMap. ConcurrentHashMap performs better because it locks a part of Map. It allows concurred read operations and the same time maintains integrity by synchronizing write operations. How ConcurrentHashMap is implemented ConcurrentHashMap was developed as alternative of Hashtable and support all functionality of Hashtable with additional ability, so called concurrency level. ConcurrentHashMap allows multiple readers to read simultaneously without using blocks. It becomes possible by separating Map to different parts and blocking only part of Map in updates. By default, concurrency level is 16, so Map is spitted to 16 parts and each part is managed by separated block. It means, that 16 threads can work with Map simultaneously, if they work with different parts of Map. It makes ConcurrentHashMap hight productive, and not to down thread-safety. Some important features of ConcurrentHashMap But you have to consider some particular qualities. Since update operation is not synchronized, simultaneous fetches data may not contains recent updates. Another thing worth mentioning is iteration over Map. Iterator returned by keySet, inconsistent and has state of ConcurrentHashMap at recent moments in... Read more
Subscribe to blog
Tags
concurrencyConcurrentHashMap
dependency injection
multithreading
pattern
singleton
SynchronousQueue
TransferQueue