When working with threads, synchronize on mutable methods becomes a valuable resource. This is because of two reasons:
A mutable method could for example be a method providing an incremental serial number based on a field instance.
Synchronize might be a valuable resource, as mentioned in item 78. However, if used incorrectly and it not being contained, it will cause major problems for the system, and eventual failure.
This is because a synchronized method continues to lock the method and its mutable objects until it has been fully executed.
If the method therefore continues around the system without any scope containment, it will eventually be impossible to say if or when it reaches itself or one of its own mutable objects. Thereby causing a concurrency deadlock or exception.
The Executor Framework provides some attractive alternatives to the normal thread. It separates the job to be done (unit of work) from the way it is done (the thread).
Executors comes in a vast number of variations, and in the right developer’s hands can perform the same tasks as any thread. However, it will require some knowledge to get into. Regardless of that, the advantages and clarity it brings to the table cannot be neglected.
With the introduction of Java 5, came the utilities of concurrent collections and synchronizers. These utilities are highly recommendable when working with multi-threading.
Both concurrent collections and synchronizers are large topics and is recommendable for any developer to investigate.
Whenever using the API of a concurrent-purpose class, it is always relevant to know to which extent this class implements thread safety. This indicates how you should implement and manage the class within your own applications context.
Generally speaking, three primary categories exists: immutable, thread safe, and not thread safe.
Regardless of the level, it should always be clearly documented on the API, so that any developer knows what to expect.
Lazy loading is the act of waiting to load or initialize an object until it is actually necessary. If it never becomes necessary, the resources of fetching it, has thereby been spared.
However, in the context of threads, it can quickly become a mess if two thread try to fetch the same object at the same time.
This issue therefore requires certain measures to be taken. Among which is to specify synchronized on the load method.
Scheduling of threads is actually handled on whatever operation system on which the java application is running on. Priorities which threads gets to run and for how long, is therefore one of the least portable features of the language.
It should therefore be well considered before distributing application with high reliance on thread scheduling to multiple operating systems. If possible avoided.
My name is Daniel H. Jacobsen and I’m a dedicated and highly motivated software developer with a masters engineering degree within the field of ICT.
I have through many years of constantly learning and adapting to new challenges, gained a well-rounded understanding of what it takes to stay up to date with new technologies, tools and utilities.
The purpose of this blog is to share both my learnings and knowledge with other likeminded developers as well as illustrating how these topics can be taught in a different and alternative manner.
If you like the idea of that, I would encourage you to sign up for the newsletter.