Should I make all my java codes thread safe?

I read some of the concurrency patterns in Brian Goetze Java concurrency in practice and was confused when it is right to ensure code security.

Usually I write code designed to run on a single thread, so I'm not too worried about security and thread synchronization, etc. However, there is always the possibility that the same code can be reused once in several - a fixed environment.

So my question is: when should you think about thread safety? Should I assume the worst at the beginning and always write thread-safe code from the very beginning, or should I review the code and change the thread safety if such a need arises later?

Are there some concurrency templates / anti-patterns that I should always be aware of even when writing single-threaded applications so that my code does not interrupt if it was later used in a multi-threaded environment?

+4
source share
4 answers

You need to think about thread safety when your code is used in a multi-threaded environment. It makes no sense to solve complexity if it is launched only in a single-threaded environment.

As the saying goes, there are simple things you can do, these are good practices, and multithreading will help:

  • As Josh Bloch says, Promote immutability . Immutable classes are thread safe almost by definition;
  • Use only data members or static variables if necessary, not convenience.
+12
source

Creating a secure code stream can be as simple as adding a comment saying that the class was not intended to be used by multiple threads at the same time. So, in that sense: yes, all of your classes should be thread safe.

However, in practice, many, many types are likely to be used by only one thread, which is often referred to only as local variables. This may be true, even if the program as a whole is multithreaded. It would be a mistake to make every object safe for multi-threaded access. Although the penalty can be small, it is ubiquitous and can be a serious fixation problem.

+4
source

I advise you to get a copy of Effective Java, 2nd ed. Joshua Bloch. This book devotes an entire chapter to concurrency, including a thorough study of when (and when not) to synchronize. Pay attention, for example, to the heading of paragraph 67 in Effective Java: Avoid Over-Synchronization, which is designed over five pages.

+2
source

As stated earlier, you need thread safety if you think your code will be used in a multi-threaded environment.

Consider the approach taken by the Collections classes, where you provide an unsafe class that does all its work without using synchronize , and you also provide another class that wraps the unsynchronized class and provides all the same public methods, but they are synchronized with the base object.

This gives your customers the ability to use a multi-threaded or single-threaded version of your code. It can also simplify your coding by isolating the entire thread / lock logic in a separate class.

+1
source

All Articles