Multiple ways of achieving Java Thread Safety
Thread safety is the process to make our program safe to use in multithreaded environment, there are different ways through which we can make our program thread safe.
· Synchronization is the easiest and most widely used tool for thread safety in java.
· Use of Atomic Wrapper classes from java.util.concurrent.atomic package. For example AtomicInteger
· Use of locks from java.util.concurrent.locks package.
· Using thread safe collection classes, check this post for usage of ConcurrentHashMap for thread safety.
· Using volatile keyword with variables to make every thread read the data from memory, not read from thread cache.
No comments:
Post a Comment