Concurrency

Java Best Practices Series

Concurrency

78. Use synchronize when handling mutable methods

When working with threads, synchronize on mutable methods becomes a valuable resource. This is because of two reasons:

  1. Synchronize ensure the method can only be accessed by one thread at a time. Any thread attempting to execute the method while it is already in use, will be put on hold until the method is available again.
  2. Because the threads are set to wait for each other, synchronize also guarantee that each thread will be able to see changes to the mutable method made by previous threads.

A mutable method could for example be a method providing an incremental serial number based on a field instance.

79. Use synchronize with caution

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.

80. Consider using executors over threads

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.

 

81. Utilize concurrent collections and synchronizers

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.

82. Document thread safety

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.

83. Use lazy load and threads with caution

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.

84. Thread schedulers are dangerous.

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.

 

Recommended Reading

Books

 

  • Video Tutorial 

    • Overview of Java Synchronizers – Douglas Schmidt – YouTube
    • Java Concurrent Collections – Bharath Thippireddy – YouTube

     

About

Hi, I'm the Author

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.

Cheers! 🍺

Didn't Find What You Were Looking For?

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Scroll to Top